2012-05-07 177 views
0

我在HTML中有兩個文本框,當單擊一個按鈕時,我需要檢查它們是否爲空。JS檢查兩個文本框是否爲空

但我一個工作,我的第二個不工作,

這裏的代碼:

<!DOCTYPE html "> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <title>appLounge webService</title> 
    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> 
    <link rel="stylesheet" type="text/css" media="all" href="css/text.css" /> 
    <link rel="stylesheet" type="text/css" media="all" href="css/960.css" /> 
    <link rel="stylesheet" type="text/css" media="all" href="css/demo2.css" /> 

<script type='text/javascript'> 
function notEmpty(login, password, helperMsg){ 
    if(login.value.length == 0){ 
     alert(helperMsg); 
     login.focus(); 
     return false; 
    } 

    if(password.value.length == 0){ 
     alert(helperMsg); 
     password.focus(); 
     return false; 
    } 

    return true; 
} 

</script> 
</head> 



<body> 
<div class="container_12"> 

<!-- login data --> 

    <div id="header" class="grid_12" style="background : url(img/login.png) no-repeat; background-size: 100%; height: 900px;" > 

<form> 
     <div id="fieldLogin1"> 
      <input type="text" id='req1' name="userName" style="width:530px; height:44px" placeholder="UserName"; /><br /> 
     </div> 
      <div class="clear">&nbsp;</div> 

     <div id="fieldLogin2"> 
      <input type="text" id='req2' name="password" style="width:530px; height:44px" placeholder="Password" /> 

     </div> 
        <div class="clear">&nbsp;</div> 

     <div id="checkBoxRememberMe"> 


        <input type="checkbox" name="remember" value="rememberYes" /> <br /> 

<form> 

<input type='button' 
    onclick="notEmpty(document.getElementById('req1'), 'req2','Please Enter a Value')" 
    value='Check Field' /> 
</form> 

     </div> 



    </div> <!-- end .grid_12 --> 
    <div class="clear">&nbsp;</div> 

</form> 

</div> 
<!-- end .container_12 --> 
</body> 
</html> 

我已經試過

onclick="notEmpty(document.getElementById('req1'), ('req2'),'Please Enter a Value')" 

爲好,但不工作或者,這是問題嗎?,

非常感謝!

回答

3
onclick="notEmpty(document.getElementById('req1'), 'req2','Please Enter a Value')" 
value='Check Field' /> 

實際上應該是

onclick="notEmpty(document.getElementById('req1'), document.getElementById('req2'),'Please Enter a Value')" 
value='Check Field' /> 

EDIT(由@dante建議的):

下面的代碼可以更可讀:

onclick="notEmpty('req1', 'req2', 'Please Enter a Value')" 
value='Check Field' /> 

function notEmpty(loginId, passwordId, helperMsg) { 
    var login = document.getElementById(loginId); 
    if (!login.value.length) { 
     alert(helperMsg); 
     login.focus(); 
     return false; 
    } 

    var password = document.getElementById(passwordId); 
    if (!password.value.length) { 
     alert(helperMsg); 
     password.focus(); 
     return false; 
    } 

    return true; 
} 

(在旁註中,儘可能習慣使用===而不是==,所以您最終不會成功(ahem ...)將布爾值與0之間的某一天進行比較。它也有點快:))

+0

...我可能會建議你把函數本身byId查找?那麼它只會是onclick ='notEmpty(「req1」,「req2」)'... – dante

+0

@dante:好點,讓我修復答案... –

+0

@dante不錯,這將如何?,請點我,謝謝! – MaKo

2

您傳遞一個字符串作爲not Empty的第二個參數。試試這個:

onclick="notEmpty(document.getElementById('req1'), document.getElementById('req2'),'Please Enter a Value')" 
2

你忘了你的密碼字段document.getElementById()當你調用該函數:

onclick="notEmpty(document.getElementById('req1'), document.getElementById('req2'),'Please Enter a Value')"