mootools 中的 keyup 停止事件不适用于 Enter 键
我有这个表单:
<div class="row border-tb">
<form id="test-form" action="/test/insert" method="post" class="form-inline offset4 span16">
<div class="box-suggest span13 border-tb">
<input type="text" name="test" value="" id="test" class="span13" placeholder="proviamo con il test"/>
</div>
<button style="margin-left: 5px;" id="send-button" type="submit" class="btn"> <i class="icon-plus"></i> </button>
</form>
</div>
以及用于捕获 keyup 的 mootools 事件:
$('test').addEvent("keyup",function(event){
event.stop();
alert("Why after this alert redirect on url: test/insert ??????");
})
我的问题是,当我按 ENTER 键时, event.stop() 不会阻止表单提交。
我也尝试过 event.stopPropagation() 和 event.preventDefault() 但什么也没有。当我按 ENTER 键时,它总是重定向到 url:“test/insert”。
为什么?
I have this form:
<div class="row border-tb">
<form id="test-form" action="/test/insert" method="post" class="form-inline offset4 span16">
<div class="box-suggest span13 border-tb">
<input type="text" name="test" value="" id="test" class="span13" placeholder="proviamo con il test"/>
</div>
<button style="margin-left: 5px;" id="send-button" type="submit" class="btn"> <i class="icon-plus"></i> </button>
</form>
</div>
and this mootools event for capture keyup:
$('test').addEvent("keyup",function(event){
event.stop();
alert("Why after this alert redirect on url: test/insert ??????");
})
My problem is that event.stop() doesn't prevent form submit when I press ENTER key.
I have try also event.stopPropagation() and event.preventDefault() but nothing. It always redirect on url: "test/insert" when I press ENTER key.
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为该事件也会在按下按键时触发,然后在表单上触发提交事件。
尝试添加以下内容:
==================
编辑
上面的内容将阻止所有表单提交,因为这可能不是您想要的,这里有一个更好的更简单的解决方案:
我之前没有写它,因为您可能实际上需要按键 UP 事件而不是 DOWN 事件,但这更有意义。
That's because the event is fired on keydown as well, and then a submit event is fired on the form.
Try to add this:
==================
Edit
The above will prevent all form submitions, since this is probably not what you're after, here's a better simpler solution:
I did not write it before since you might actually need the key UP event and not DOWN, but this just makes more sense.