我有以下简单的 javascript 代码,它处理返回键,我不想在文本框中按下返回键时提交表单。
所有这些工作正常,但在 Firefox 中,如果我显示一条警报消息,那么它会停止工作并且开始提交表单,而没有警报消息的确切代码工作正常并停止提交表单。我不明白为什么警报会破坏聚会。
$("document").ready(function () {
$("#input1").keydown(OnKeyDown);
});
function OnKeyDown(e) {
if (e.keyCode == 13) {
// alert('this will fail'); // Adding alert makes the form submit
stopBubble(e);
return false;
}
}
function stopBubble (e) {
// If an event object is provided, then this is a non-IE browser
if (e && e.stopPropagation)
// and therefore it supports the W3C stopPropagation() method
e.stopPropagation();
else
// Otherwise, we need to use the Internet Explorer
// way of cancelling event bubbling
window.event.cancelBubble = true;
}
<input type="text" id="input1" value="">
我真的不知道该事件是否正常化。但这就是我必须这样做才能使其在所有浏览器中工作的方式:
$(whatever).keypress(function (e) {
var k = e.keyCode || e.which;
if (k == 13) {
return false; // !!!
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)