如何防止在非表单输入字段上按回车键时发送表单? (深度神经网络)
在页面上,我有一个谷歌搜索字段和一个单独的登录表单。为了使搜索字段与输入一起工作,我添加了以下脚本:
$('#searchBox').keydown(function (event) {
if (event.keyCode == 13) {
document.location.href = "someTargetPage.html";
}
});
唯一的问题是,在这种情况下,将发送表单,因为搜索字段包含在表单中,而我无法更改它,因为点网核武器的架构。我试图像这样防止这种情况:
$('form').delegate('input:submit', 'click',
function () {
return false;
});
现在搜索字段可以很好地与输入一起使用,但是表单中的提交按钮不起作用!有没有办法检查触发器的来源并允许或拒绝它?
感谢您的任何提示!
On a page I have a google search-field and a separate form for a login. In order to make the search-field work with enter, I included the following script:
$('#searchBox').keydown(function (event) {
if (event.keyCode == 13) {
document.location.href = "someTargetPage.html";
}
});
The only problem is that in that case the form would be sent because the search-field is included within the form, which I can't change due to the architecture of dot net nuke. I tried to prevent that like this:
$('form').delegate('input:submit', 'click',
function () {
return false;
});
Now the search-field does work nicely with enter, but the submit-button from the form won't work! Is there a way to check from where the trigger comes and either allow or deny it?
Thx for any tipps!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除阻止输入按钮工作的代码(
input:submit
上的委托)。您只需使#searchBox
不将事件传播到表单即可。搜索框处理程序需要通过返回 false 来取消事件:Remove your code that stops the input button from working (your delegate on
input:submit
). You just need to make#searchBox
not propagate the event up to the form. It's the search box handler that needs to cancel the event by returning false:如果您确实想完全禁用该表单,只需在其 onsubmit 事件上返回 false 即可:
if you actually want to totally disable the form, just return false on it's onsubmit event: