JavaScript获取html表单值验证后跳转网页中的关键点
关键代码:
1.表单部分
<form action="Depart.jsp" name="myform" method="post" onsubmit="return check()">
<div>
<h1>登陆</h1>
<input type="text" name="id" id="id" required lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
<input type="password" name="pas"id="pas" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
<button type="submit" class="layui-btn" >登陆</button>
<button type="submit" class="layui-btn " >注册</button>
</div>
</form>
2.js代码部分
<script>
function check(){
var name=document.forms["myform"]["id"].value;
if(name=="tea1")
{
alert("将进入校稿人页面");
window.location.href="First.jsp";
return false;
}else if(name=="tea2"){
alert("将进入审稿人页面");
window.location.href="Second.jsp";
return false;
}
}
</script>
3.js获取表单值关键点:
(1) var name=document.forms["myform"]["id"].value; 获取对应值在表单的位置要对应正确
(2)遇到问题耐心找方法onsubmit="return check()这段代码要加在<form>标签内
4.js实现网页跳转到与<form action="sss.jsp">不同于sss.jsp页面的方法关键点
(3)onsubmit="return check()这段代码要加在<form>标签内
(4)且check()返回值为false,若return true,页面会跳转到action="Depart.jsp"里的页面