方法一:JS判断提交
function checkUser(){ var result = document.getElementById("userid").value; var password = document.getElementById("userpassid").value; if(result == "" ){ alert("用户名不能为空"); return false; } if(password == "" ){ alert("密码不能为空"); return false; }else{ return true; } }
form表单
<form id="formid" name="myform" method='post' onsubmit="return checkUser();" > <input type="text" value="" class="text2" name="username" id="userid"/></td> <input type="password" value="" class="text2" name="userpass" id="userpassid"/></td> <input type="submit" value="" class="btn2" /> </form>
方法二:
function checkUser(){ var result = document.getElementById("userid").value; var password = document.getElementById("passid").value; if(!result){ alert("用户名不能为空"); return false; } if(!password){ alert("密码不能为空"); return false; } document.getElementById("formid").submit(); }
form表格的写法,需要写id
<form id="formid" method='post'>
button按钮的写法如下:
<input type="button" value="" class="btn2" onclick="checkUser();" />