表单事件嵌套,不好描述进来看吧:)
我在做个表单域的验证,比如如果他的value是“faint”,就选中faint,不让鼠标离开。但用户可以用ctrl + enter,跳过验证提交。
code如下:
<script>
function test(obj){
if(obj.value== "faint ") {
alert( "something wrong ");
obj.select();
return false;
}
return true;
}
function b(){ alert( "b ");return false;}
</script>
<body>
<form id= "testForm ">
<input type=text value= "faint " onblur= "test(this); " /> <br/>
<input type=text /> <br/>
<input type=submit />
</form>
</body>
我的解决办法:在表单submit的时候重复验证字段,但又有新的问题,这样就先执行onblur ,然后执行onsubmit,错误框弹出2次。
如何解决这个问题呢?大虾支招
<script>
function test(obj){
if(obj.value== "faint ") {
alert( "something wrong1 ");
obj.select();
return false;
}
return true;
}
function checkForm(){
var obj = document.getElementById( "txt ");
if(obj.value== "faint ") {
alert( "something wrong2 ");
obj.select();
return false;
}
}
</script>
<body>
<form id= "testForm " onsubmit= "return checkForm() ">
<input type=text name=txt value= "faint " onblur= "test(this); " /> <br/>
<input type=text /> <br/>
<input type=submit />
</form>
</body>
------解决方案--------------------onsubmit判断一次就可以了,不过里面的代码不要写错,报错了的话就直接跳过去不验证了
------解决方案--------------------禁止 Enter 提交页面,不过如果用户禁用脚本的话,一切都白搭,最好回 Server 再验证一次!
代码如下,L@_@K
<script language= "javascript " event= "onkeydown " for= "document ">
// 将 Enter 键 设置为 Tab 键
if ( event.keyCode == 13 )
{
event.keyCode = 9;
}
</script>
<script>
function test(obj){
if(obj.value== "faint ") {
alert( "something wrong1 ");
obj.select();
return false;
}
return true;
}
function checkForm(){
var obj = document.getElementById( "txt ");
if(obj.value== "faint ") {
alert( "something wrong2 ");
obj.select();
return false;
}
}
</script>
<body>
<form id= "testForm " onsubmit= "return checkForm() ">
<input type=text name=txt value= "faint " onblur= "test(this); " /> <br/>
<input type=text /> <br/>
<input type=submit />
</form>
</body>