javascript调用问题,form中有action和onsubmit,但是onsubmit中返回false,依然执行action
代码如下:<scripttype="text/javascript">functioncheckForm(){alert("invoked!");varf=focumen...
代码如下:
<script type="text/javascript">
function checkForm() {
alert("invoked!");
var f = focument.forms[0];
if (f.username.value = ='') {
document.getElementById("username_info").style.display = "block";
return false;
}
if (f.password.value = = '') {
document.getElementById("password_info").style.display = "block";
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" onsubmit="return checkForm()">
<span> User_Name: <input type="text" name="username">
<span id="username_info" width="40" length="20"
style="display: none"> <font color="green" bgcolor="black">用户名不能为空</font>
</span> </span>
<br>
<div>
Password:
<input type="password" name="password">
<div id="password_info" width="40" length="20" style="display: none">
<font color="red">密码不能为空
</div>
</div>
<input type="submit" value="submit">
</form>
</body>
谢谢哥们细心回答,不过貌似这不是问题根本哦,引文function checkForm()中第一句就没有执行,alert("invoked!")
还有form里那个action换成"#"的话,点提交之后地址栏里会变成http://localhost:8080/test/#,总之就是这个JS方法没被调用,纠结ING 展开
<script type="text/javascript">
function checkForm() {
alert("invoked!");
var f = focument.forms[0];
if (f.username.value = ='') {
document.getElementById("username_info").style.display = "block";
return false;
}
if (f.password.value = = '') {
document.getElementById("password_info").style.display = "block";
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" onsubmit="return checkForm()">
<span> User_Name: <input type="text" name="username">
<span id="username_info" width="40" length="20"
style="display: none"> <font color="green" bgcolor="black">用户名不能为空</font>
</span> </span>
<br>
<div>
Password:
<input type="password" name="password">
<div id="password_info" width="40" length="20" style="display: none">
<font color="red">密码不能为空
</div>
</div>
<input type="submit" value="submit">
</form>
</body>
谢谢哥们细心回答,不过貌似这不是问题根本哦,引文function checkForm()中第一句就没有执行,alert("invoked!")
还有form里那个action换成"#"的话,点提交之后地址栏里会变成http://localhost:8080/test/#,总之就是这个JS方法没被调用,纠结ING 展开
2个回答
展开全部
var f = focument.forms[0];
- -
这里写错了
是document.forms[0];
var f = document.forms[0];
-------------------
我粘贴过来就执行了呢= =
--------------------
action =“” 默认就是提交到本页~
你加个‘#’~那还是提交到本页- - 就是这样咯。
-----------------------------------------------------
好吧,我再把我修改过的代码粘过来,你代码里面还有一些其他小问题。。
---------------------------------------------------------
<script type="text/javascript">
function checkForm() {
alert("invoked!");
var f = document.forms[0];
if(f.username.value == '') {
document.getElementById("username_info").style.display = "block";
return false;
}
if(f.password.value == '') {
document.getElementById("password_info").style.display = "block";
return false;
}
return true;
}
</script>
<form action="" method="post" onsubmit="return checkForm()">
<span> User_Name: <input type="text" name="username">
<span id="username_info" width="40" length="20"
style="display: none"> <font color="green" bgcolor="black">用户名不能为空</font>
</span> </span>
<br>
<div>
Password:
<input type="password" name="password">
<div id="password_info" width="40" length="20" style="display: none">
<font color="red">密码不能为空
</div>
</div>
<input type="submit" value="submit">
</form>
----------------------------------------------------
‘==’ 中间不能有空格~
- -
这里写错了
是document.forms[0];
var f = document.forms[0];
-------------------
我粘贴过来就执行了呢= =
--------------------
action =“” 默认就是提交到本页~
你加个‘#’~那还是提交到本页- - 就是这样咯。
-----------------------------------------------------
好吧,我再把我修改过的代码粘过来,你代码里面还有一些其他小问题。。
---------------------------------------------------------
<script type="text/javascript">
function checkForm() {
alert("invoked!");
var f = document.forms[0];
if(f.username.value == '') {
document.getElementById("username_info").style.display = "block";
return false;
}
if(f.password.value == '') {
document.getElementById("password_info").style.display = "block";
return false;
}
return true;
}
</script>
<form action="" method="post" onsubmit="return checkForm()">
<span> User_Name: <input type="text" name="username">
<span id="username_info" width="40" length="20"
style="display: none"> <font color="green" bgcolor="black">用户名不能为空</font>
</span> </span>
<br>
<div>
Password:
<input type="password" name="password">
<div id="password_info" width="40" length="20" style="display: none">
<font color="red">密码不能为空
</div>
</div>
<input type="submit" value="submit">
</form>
----------------------------------------------------
‘==’ 中间不能有空格~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询