所以我有一个登录表单,我想测试输入是否为空。 我没有收到错误,但是当我提交它时只是忽略了onsubmit。 帮助
这是我的表格:
<form onsubmit="return ifEmpty();" action="login1.php" method="post" id="loginform" accept-charset="UTF-8">
<div style="vertical-align: top;" dir="rtl">
<label dir="rtl" class="labels">שם משתמש/אימייל</label>
<br>
<br>
<input name="unameemail" id="username" type="text">
</div>
<div style="display: block;vertical-align:top;" dir="rtl">
<br>
<label dir="rtl" class="labels">סיסמה</label>
<br>
<br>
<input name="password" id="passwordin" type="password">
</div>
<div>
<br>
<br>
<br>
<input class="btn" type="submit" name="submitone" onclick="" value="login">
</div>
</form>
</div>
和我的js:
<script type="text/javascript">
function ifEmpty() {
if (document.getElementById("username") == "") {
document.getElementById("username").style.backgroundColor = "red";
document.getElementById("username").style.color = "white";
document.getElementById("username").className += " invalidform";
return false;
} else if (document.getElementById("passwordin") == "") {
document.getElementById("username").style.backgroundColor = "white";
document.getElementById("username").style.color = "black";
document.getElementById("password").style.backgroundColor = "red";
document.getElementById("password").style.color = "white";
document.getElementById("password").className += " invalidform";
return false;
}
}
</script>
答案 0 :(得分:1)
变化:
if (document.getElementById("username") == "") {
到此:
if (document.getElementById("username").value == "") {
答案 1 :(得分:1)
if(document.getElementById(&#34; username&#34;)==&#34;&#34;){
您正在将DOM元素与字符串进行比较; - )
您需要访问输入元素的value属性。