简体   繁体   English

如果将按钮放入SubmitHandler,为什么我的按钮在jquery中单击两次?

[英]Why does my button click twice in jquery if I put it in submitHandler?

I have a short form, you can see it here http://jsfiddle.net/azxpckg5/1/ 我有一个简短的表格,您可以在这里看到它http://jsfiddle.net/azxpckg5/1/

and I have a problem - the way to reproduce it is to click the save button. 我有一个问题-再现它的方法是单击“ save按钮。 Then there will appear another button called submit . 然后将出现另一个名为submit按钮。 when user clicks it - it disappears and it's fine. 当用户单击它时-它消失了就可以了。 But when user repeats this procedure (clicks save again and submit again - he can see that the last click was repeated twice. I believe the error might be somewhere here: 但是,当用户重复此过程时(再次单击“ save并再次submit -他可以看到最后一次单击重复了两次。我相信错误可能在这里:

submitHandler: function (form) {
        alert("here!");
        $(".overlay-boxify2").toggleClass("open");
        $('#submitcForm').click(function() {
   // 
         $(".overlay-boxify2").toggleClass("open");
            alert("hegdsgsd");
        });
        return false;
    }

but to be honest I don't know how to fix it and what can be the issue. 但老实说,我不知道如何解决它,可能是什么问题。 Can you help me with that? 你能帮我吗?

The issue is because you're attaching another click event handler to the #submitcForm button on every submission of the form (which happens when #saveBtn is clicked. Move the click handler outside of the validate() call and your code will work as you require. 问题是因为您要在表单的每个提交#submitcForm另一个click事件处理程序附加到#submitcForm按钮上(在单击#saveBtn时发生。将单击处理程序#saveBtn validate()调用之外,您的代码将按您的方式工作要求。

$('#invoiceForm').validate({
    // settings...
});

$('#submitcForm').click(function () {
    $(".overlay-boxify2").toggleClass("open");
});

Updated fiddle 更新的小提琴

Use .off() to prevent attaching multiple click eventListener on your button 使用.off()防止在您的按钮上附加多个click eventListener

submitHandler: function (form) {
    alert("here!");
    $(".overlay-boxify2").toggleClass("open");
    $('#submitcForm').off().click(function() { // see the use of .off()
        $(".overlay-boxify2").toggleClass("open");
        alert("hegdsgsd");
    });
    return false;
}

See more about .off() 进一步了解.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM