如何防止在非表单输入字段上按回车键时发送表单? (深度神经网络)

发布于 2024-12-18 20:35:46 字数 493 浏览 2 评论 0原文

在页面上,我有一个谷歌搜索字段和一个单独的登录表单。为了使搜索字段与输入一起工作,我添加了以下脚本:

$('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷梦 2024-12-25 20:35:46

删除阻止输入按钮工作的代码(input:submit 上的委托)。您只需使 #searchBox 不将事件传播到表单即可。搜索框处理程序需要通过返回 false 来取消事件:

$('#searchBox').keydown(function (event) {
    if (event.keyCode == 13) {
        document.location.href = "someTargetPage.html";
        return 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:

$('#searchBox').keydown(function (event) {
    if (event.keyCode == 13) {
        document.location.href = "someTargetPage.html";
        return false;
    }
});
我的奇迹 2024-12-25 20:35:46

如果您确实想完全禁用该表单,只需在其 onsubmit 事件上返回 false 即可:

$("#searchform").live("onsubmit", function(){ return false; });

if you actually want to totally disable the form, just return false on it's onsubmit event:

$("#searchform").live("onsubmit", function(){ return false; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文