Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数的作用是:在表单输入过程中,不阻止多次单击事件_Javascript_Jquery - Fatal编程技术网

Javascript 函数的作用是:在表单输入过程中,不阻止多次单击事件

Javascript 函数的作用是:在表单输入过程中,不阻止多次单击事件,javascript,jquery,Javascript,Jquery,我有这样一个代码: $(document).ready(function() { $("#some-items").one("click", ".comment-form-gonder input", function(e) { e.preventDefault(); $("body").css("cursor", "wait"); //this part inserts user comment to the written comments below with

我有这样一个代码:

$(document).ready(function() {
  $("#some-items").one("click", ".comment-form-gonder input", function(e) {
    e.preventDefault();
    $("body").css("cursor", "wait");

    //this part inserts user comment to the written comments below with animation.

    ...

问题是,当我双击发送按钮时,注释会插入两次。如何防止这种情况发生?

尝试删除preventDefault;和 禁用按钮以确保

$(this).attr('disabled', 'disabled');
var n=0 $div.一次点击,功能{ n++; 如果n%2==0{ $this.text'Foo'; }否则{ $this.text'Bar'; } }; Foo调用e.default;在处理程序中,使窗体在执行第一类“事件”后无法阻止默认行为。之后,同一事件将按预期提交表单

使用on阻止默认表单提交,并使用one完成其余工作:

您也可以在第一次单击后禁用提交输入,但在某些情况下,可能需要将其保持为“可单击”f.ex,以通知用户不能提交表单两次。为此,您可以将单击状态存储在输入的数据属性中,并在以后使用该状态:

$(".comment-form-gonder input[type='submit']").on("click", function(e) {
    e.preventDefault();
    if($(this).data('clicked') == 'clicked'){
        return alert('already submitted!');
    }
    // This part executes only on first click:
    $(this).data('clicked', 'clicked');
    $("body").css("cursor", "wait");
    // rest of your code...
});

或者this.disabled=true最有可能是您的事件在DOM中冒泡。尝试调用e.stopPropagation,而不是preventDefault。这将确保事件不会触发预期元素之上的其他元素。
$(".comment-form-gonder input[type='submit']").on("click", function(e) {
    e.preventDefault();
    if($(this).data('clicked') == 'clicked'){
        return alert('already submitted!');
    }
    // This part executes only on first click:
    $(this).data('clicked', 'clicked');
    $("body").css("cursor", "wait");
    // rest of your code...
});