2010-04-18 67 views
2

我期待的是如果用户填写表单,它将提供对新位置的访问。JavaScript加载新页面问题

<script language="JavaScript" type="text/javascript">  
<!--  
function validateForm(theForm) {  
var firstname = theForm.firstname.value; 
var lastname = theForm.lastname.value;  
var email = theForm.email.value;  
if (firstname == "") {  
    alert("Please fill in your First Name.");  
    theForm.firstname.focus();  
    return false;  
} 
if (lastname == "") {  
    alert("Please fill in your Last Name.");  
    theForm.lastname.focus();  
    return false;  
} 

if (email == "") {  
    alert("Please fill in your email address.");  
    theForm.email.focus();  
    return false;  
}  
return true;  
} 

我知道这部分是错误的,但我不知道如何去做。任何帮助将是不错..

if lastname="" 
if firstname="" 
if email="" 
load('www.google.com'); 

回答

3
if (validateForm(theForm)) window.location = 'http://www.google.com'; 

等同于使用

if (validateForm(theForm)) window.location.href = 'http://www.google.com'; 

双方将工作,所以选择你喜欢哪一个。

+0

我现在的用户需要键入某种放置在输入框中数据的方式....或者什么关系呢? – Michael 2010-04-18 06:12:26

+0

好的,我会更改我的代码以包含 – 2010-04-18 06:13:12

+0

谢谢...我欣赏帮助。 – Michael 2010-04-18 06:48:44

0
if lastname="" 
if firstname="" 
if email="" 
load('www.google.com'); 

成为

if ((lastname == "") && (firstname == "") && (email == "")) { 
    window.location = "http://www.google.com"; 
} 
+1

围绕'lastname ==“”'(等)的括号是不必要的。 – cletus 2010-04-18 06:22:12

+0

@cletus但不正确。用括号将条件中的每个部分都包含起来,可以使代码更具可读性。 – GlenCrawford 2010-04-18 06:37:25

+1

我不同意。不必要的括号有时会使代码更难读。有人在阅读时会看到括号,然后不得不问“为什么他们在那里?”,再读一遍,看看他们是否错过了一些东西。但你是对的,因为它是合法的代码。 – cletus 2010-04-18 06:47:30