简体   繁体   English

jQuery中触发错误按钮的Click事件?

[英]Click event for the wrong button being fired in jQuery?

This happens very rarely but it still happens sometimes. 这种情况很少发生,但有时仍会发生。 I have two buttons next to each others with a jQuery click event on each: 我有两个彼此相邻的按钮,每个按钮都有一个jQuery click事件:

JS: JS:

$("#accepttrade").click(function(){
    if(document.getElementById("agreeterms").checked ){
        //accept process
        $("#acceptdeposit").slideUp(200);
    }

});

$("#declinetrade").click(function(){
    //decline
    $("#acceptdeposit").slideUp(200);

});

HTML: HTML:

<div id="acceptdeposit">
    <button id="declinetrade" >Decline</button>&emsp;
    <button id="accepttrade" >Accept</button><input type="checkbox" id="agreeterms">
</div>

But sometimes when someone click on decline, it occurs the click of accept button, and go through even if the checkbox is unchecked. 但是有时,当有人单击拒绝时,它会单击接受按钮,即使未选中该复选框也要经过。

I have never experienced it myself, but is it possible that this could happend? 我本人从未经历过,但是有可能发生这种情况吗? How can I be sure that "accept process" is never reached unless the user checks the box and click on accept? 我如何确定除非用户选中该框并单击“接受”,否则就永远不会达到“接受过程”?

Try this, cancel your click event: 试试这个,取消您的点击事件:

$("#accepttrade").click(function(e){
   if($("#agreeterms").is(':checked') ){
       //accept process
       $("#acceptdeposit").slideUp(200);
   }
  e.preventDefault();
});    

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

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