表单:给表单加上
οnsubmit="return submitCheck();"
<form action="index.php" method="post" οnsubmit="return submitCheck();">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="name" name="name" placeholder="请输入用户名" class="txt"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" id="pwd" name="pwd" placeholder="请输入密码" class="txt"></td>
</tr>
<tr>
<td>验证码:</td>
<td>
<input type="text" id="yzm" name="yzm" size="10" placeholder="请输入验证码" class="yzm">
<img id="code" class="img_yzm" src="./public/common/yanzhengma.php" οnclick="this.src+='?'"/> </td>
</tr>
<tr>
<td colspan="2"><center><input type="radio" value="普通用户" name="sf" checked="checked"/>普通用户
<input type="radio" value="管理员" name="sf"/>管理员</center></td>
</tr>
<tr>
<td colspan="2"><center>
<input type="submit" name="login" value="登录" class="btn" id="btn1">
<input type="reset" name="ret" value="重置" class="btn" id="btn2">
</center></td>
</tr>
</table>
</form>
js页面不刷新判断输入是否为空:
<script type="text/javascript">
// 表单元素提交时JS判断输入的值,为空时则不刷新页面返回,不为空跳转并传值到新页面
function submitCheck(){
var txt1 = document.getElementById("name");
var txt2 = document.getElementById("pwd");
var txt3 = document.getElementById("yzm");
if(txt1.value==""){
alert("请输入用户名!");
txt1.focus();
return false;
}else if(txt2.value==""){
alert("请输入密码!");
txt2.focus();
return false;
}else if(txt3.value==""){
alert("请输入验证码!");
txt3.focus();
return false;
}
return true;
}
</script>