javascript当中表单提交(空格提交的问题)
4.表单提交(空格提交的问题)
例 4.1(form.submitIEFF.html)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script language=javascript>
function check()
{
var form = ElementById("regForm");
if (form.user.value == "")
{
alert("⽤户名不能为空!");
}
else
{
form.submit();
}
}
</script>
<form method=post id="regForm" action="jsp1.jsp">
⽤户<input type="text" name="user"/><br>
<INPUT TYPE="button" οnclick="check();" id="regBut" value="提交"/>
</form>
以上例⼦很好,但有个问题,当光标放在⽂本框⾥时,即使空格,回车也会提交。不信你试试,浏览器(IE和⽕狐)都这样。下⾯给出解决办法。
例 4.1_a
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script language=javascript>
function check()
{
var form = ElementById("regForm");
if (form.user.value == "")
{
alert("⽤户名不能为空!");
}
else
{
html中提交表单用什么属性form.submit();
}
}
</script>
<form method=post id="regForm" action="jsp1.jsp">
⽤户<input type="text" name="user" οnkeydοwn="if(event.keyCode==13) return false;"/><br>
<INPUT TYPE="button" οnclick="check();" id="regBut" value="提交"/>
</form>
或者⽤下⾯的例⼦,⾥⾯⽤了onSubmit,只要提交,它就会被执⾏。
例 4.2(onSubmitReturnIEFF.html)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script language=javascript>
function check(form)
{
/*onSubmit (Event handler)
The user has clicked on the submit button in a form.
Property/method value type: Boolean primitive
JavaScript syntax: - submit = aHandler
HTML syntax: <FORM onSubmit="..."> <HTMLTag onSubmit="...">
As the <FORM> submit button is clicked, this event is triggered.If you return the value true, the event will be passed to the browser for further processing and the form will be submitted.
Returning false inhibits this action and discards the message;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论