当 Enter 键已被禁用时,jQuery 在按下 Enter 键后禁用 Enter 键

发布于 2024-12-06 19:12:34 字数 1711 浏览 2 评论 0原文

我已经禁用了回车键,以便可以执行 ajax 提交,但我想确保如果用户在服务器响应返回之前按两次回车键,则表单不会提交两次。

在这里,我禁用了 Enter 键并为其分配了一个名为 submitForm() 的函数:

$(document).ready(function(){
    offset = $("#quantity").html();
    $("#lastName").focus();
    $(document).bind("keypress" , function(e){
        if (e.which == 13 && (!$("#notes").is(":focus")) && (!$("#submit").is(":focus"))) {
            submitForm(); return false;
        }
    })
    $("#submit").click(function(){ submitForm(); });
});

我尝试在其他地方再次 bind(),但它没有执行任何操作。我在不同的地方尝试了 preventDefault() ,但我认为我要么把它放在错误的地方,要么这是错误的做法。我似乎无法让它发挥作用。

我发现了这个: 如何在执行函数后禁用 Enter/Return 键?,但它没有回答我的问题,因为在海报的示例中,脚本检查框中是否有任何内容,但我想检查一下我是否已提交或未提交。我不知道该怎么做。我真正想做的是禁用回车键,直到我收到服务器对 ajax 调用的回复。这是submitForm():

function submitForm() {
    $.post("/ajax/files" , $("#files").serialize(), function(data){
        filesReset();
        if (data != "") {
            $("#lastInsert table tbody:last").prepend(data);
            $("#lastInsert table tbody:last tr:first").find("td").hide();
            $("#lastInsert table tbody:last tr:first").find("td").fadeIn(1000);
            $("#lastInsert table tbody:last tr:first").effect("highlight" , {"color": "aqua"}, 1000);

            $("#lastInsert table tbody:last tr:last").remove();
        } else {
            alert("Insert rejected: either insufficient criteria or the server is down.");
            $("#hidden").click();
        }
    });
}

我不想在服务器端做任何事情,因为我需要这个提交功能尽可能快(这将是一个快节奏的数据输入表单)。

I have disabled my enter key so that I can do an ajax submission, but I want to make sure that the form isn't submitted twice if the user hits enter twice before the response from the server comes back.

Here I have disabled the enter key and assigned a function to it called submitForm():

$(document).ready(function(){
    offset = $("#quantity").html();
    $("#lastName").focus();
    $(document).bind("keypress" , function(e){
        if (e.which == 13 && (!$("#notes").is(":focus")) && (!$("#submit").is(":focus"))) {
            submitForm(); return false;
        }
    })
    $("#submit").click(function(){ submitForm(); });
});

I tried to bind() again elsewhere, but it didn't do anything. I tried preventDefault() in various places, but I think I'm either putting it in the wrong place, or else that's the wrong thing to do. I can't seem to get this to work.

I found this: How to disable Enter/Return Key After a function is executed because of it?, but it doesn't answer my question because in the poster's example, the script checks to see whether there is anything in the box, but I want to check to see whether I have submitted or not. I'm not sure how to do that. What I would really like to do is disable the enter key until I hear back from the server on the ajax call. Here is submitForm():

function submitForm() {
    $.post("/ajax/files" , $("#files").serialize(), function(data){
        filesReset();
        if (data != "") {
            $("#lastInsert table tbody:last").prepend(data);
            $("#lastInsert table tbody:last tr:first").find("td").hide();
            $("#lastInsert table tbody:last tr:first").find("td").fadeIn(1000);
            $("#lastInsert table tbody:last tr:first").effect("highlight" , {"color": "aqua"}, 1000);

            $("#lastInsert table tbody:last tr:last").remove();
        } else {
            alert("Insert rejected: either insufficient criteria or the server is down.");
            $("#hidden").click();
        }
    });
}

I would prefer not to have to do anything on the server-side, because I need this submit functionality to be as fast as possible (this will be a fast paced data entry form).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

拥有 2024-12-13 19:12:34

使用这样的变量:

$(function(){

    var running = false;

    $(document).bind("keypress" , function(e){
        if (e.which == 13) {
            if(running === false) {
                running = true;
                submitForm();
            }
            return false;
        }
    })

    $("#submit").click(function(){
        if(running === false) {
            running = true;
            submitForm(); 
        }
    });

});

Make use of a variable like this:

$(function(){

    var running = false;

    $(document).bind("keypress" , function(e){
        if (e.which == 13) {
            if(running === false) {
                running = true;
                submitForm();
            }
            return false;
        }
    })

    $("#submit").click(function(){
        if(running === false) {
            running = true;
            submitForm(); 
        }
    });

});
死开点丶别碍眼 2024-12-13 19:12:34

将您的提交按钮更改为常规按钮并使用onclick事件通过JavaScript提交表单会更容易。这样按 Enter 键什么也不做。单击按钮时禁用该按钮,并在 Ajax 调用成功时启用它。

It's easier just to change your submit button you a regular button an use the onclick event to submit the form using JavaScript. That way pressing enter does nothing. Disable the button when it's clicked and enable it on success of your Ajax call.

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