2010-04-18 59 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