这是我正在使用的测试页面。这个版本工作正常,转发到#success:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<script type="text/javascript" src="prototype.js"></script>
</head><body>
<form id='form' method='POST' action='#fail'>
<button id='button'>Oh my giddy aunt!</button>
<script type="text/javascript">
var fn = function() {
$('form').action = "#success";
$('form').submit();
}
$('button').observe('mousedown', fn);
</script>
</form>
</body></html>
如果我清空处理程序:
var fn = function() {
}
表单已提交,但我们这次会被发送到#fail。
在处理程序中发出警报:
var fn = function() {
alert("omg!");
}
表单未提交。这非常好奇。
使用event.stop()
,这应该阻止浏览器采取默认操作:
var fn = function(event) {
event.stop();
}
我们被发送到#fail。
因此alert()
在阻止提交方面比event.stop()
更有效。是什么给了什么?
我正在使用Firefox 3.6.3和Prototype 1.6.0.3。此行为也出现在Prototype 1.6.1中。
答案 0 :(得分:2)
您无法使用Event.stop(event)
停止表单提交的原因是您正在观察的事件是mousedown事件,而不是form.submit()事件。我猜测alert()阻止提交的原因是因为你在输入类型=“按钮”上覆盖了mousedown的默认行为
答案 1 :(得分:0)
它看起来像你的event.stop中的'e'需要大写(读取Event.stop();)
答案 2 :(得分:0)
似乎提交表单的原因是因为我还没有抓住点击。