我在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"> </div>
<div id="fieldLogin2">
<input type="text" id='req2' name="password" style="width:530px; height:44px" placeholder="Password" />
</div>
<div class="clear"> </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"> </div>
</form>
</div>
<!-- end .container_12 -->
</body>
</html>
我已經試過
onclick="notEmpty(document.getElementById('req1'), ('req2'),'Please Enter a Value')"
爲好,但不工作或者,這是問題嗎?,
非常感謝!
...我可能會建議你把函數本身byId查找?那麼它只會是onclick ='notEmpty(「req1」,「req2」)'... – dante
@dante:好點,讓我修復答案... –
@dante不錯,這將如何?,請點我,謝謝! – MaKo