aguang110 发表于 2013-1-29 09:31:47

event.returnValue和return false的区别


[*]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
[*]<html xmlns="http://www.w3.org/1999/xhtml">   
[*]<title>runcode</title>   
[*]<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
[*]<meta name="Author" content="Sheneyan" />   
[*]<head>   
[*]<script type="text/javascript">   
[*]function form1(){   
[*]event.returnValue = false;   
[*]}   
[*]function form2(){   
[*]return false;   
[*]}   
[*]</script>   
[*]</head>   
[*]<body>   
[*]<form onsubmit="form1()">  <!-- 1 -->   
[*]<fieldset><legend>event.returnValue = false</legend> <input   
[*]    type="submit" /></fieldset>   
[*]</form>   
[*]<form onsubmit="return form2()"> <!-- 2 -->   
[*]<fieldset><legend>return false的正确用法</legend> <input   
[*]    type="submit" /></fieldset>   
[*]</form>   
[*]<form onsubmit="form2()"> <!-- 3 -->   
[*]<fieldset><legend>return false的错误用法</legend> <input   
[*]    type="submit" /></fieldset>   
[*]</form>   
[*]</body>   
[*]</html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><title>runcode</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="Author" content="Sheneyan" /><head><script type="text/javascript">function form1(){event.returnValue = false;}function form2(){return false;}</script></head><body><form ><!-- 1 --><fieldset><legend>event.returnValue = false</legend> <inputtype="submit" /></fieldset></form><form > <!-- 2 --><fieldset><legend>return false的正确用法</legend> <inputtype="submit" /></fieldset></form><form > <!-- 3 --><fieldset><legend>return false的错误用法</legend> <inputtype="submit" /></fieldset></form></body></html> 
   三个提交的onsubmit属性都是想让表单提交之前验证,验证失败则不提交表单。第一、二两处做到了,第三出没做到。
   这个只有运行起来才能看到结果
   1处,表单没有提交
   2处,表单没有提交
   3处,表单提交了。这不是希望的
 
event.returnValue的作用就是:当捕捉到事件(event)时,做某些判断,如果判断失败,则阻止当前事件继续运行,这样讲您也许还不是特别理解,我再举一个例子,该例子达到的效果是:不能在一个输入框中输入小数。
FireFox浏览器:
<div class="dp-highlighter"><div class="bar"><div class="tools">Js代码 http://www.agoit.com/images/icon_copy.gif http://www.agoit.com/images/icon_star.pnghttp://www.agoit.com/images/spinner.gif
页: [1]
查看完整版本: event.returnValue和return false的区别