如何退出点击功能?

时间:2016-01-22 17:55:21

标签: javascript jquery ajax

所以在这个例子中,我有一个带有隐藏字段的表单和一个名为ban user的按钮。单击禁用用户按钮时,它会在隐藏字段中提交值,并将ajax请求发送到java servlet。如果成功,则禁止用户,并将按钮更改为" unban user"。问题是,当我单击按钮一次并禁止用户并尝试再次单击它以取消禁用时,我仍然在禁止用户的点击事件中,我收到警报"你确定你想要禁止ID为...的用户?"。如何退出click事件以确保何时再次单击该按钮,它是从函数的开头而不是在click函数内部开始的?我尝试过使用' return;'正如你在下面看到的那样但是没有用。

$(document).delegate('form', 'click', function() {
var $form = $(this);
var id = $form.attr('id');
var formIdTrim = id.substring(0,7);
if(formIdTrim === "banUser") {
     $(id).submit(function(e){
        e.preventDefault();
     });

     var trimmed = id.substring(7);
     var dataString = $form.serialize();
     var userID = null;
     userID = $("input#ban"+ trimmed).val();

     $("#banButton"+ trimmed).click(function(e){
         e.preventDefault();

         //get the form data and then serialize that


         dataString = "userID=" + userID;

         // do the extra stuff here
         if (confirm('Are you sure you want to ban the user with the id of ' + trimmed +'?')) {
             $.ajax({
                 type: "POST",
                 url: "UserBan",
                 data: dataString,
                 dataType: "json",
                 success: function(data) {
                     if (data.success) {
                        //$("#banUser"+trimmed).html("");
                        $('#banUser'+trimmed).attr('id','unbanUser'+trimmed);
                        $('#ban'+trimmed).attr('id','unban'+trimmed);
                        $('#banButton'+trimmed).attr('value',' UnBan User ');
                        $('#banButton'+trimmed).attr('name','unbanButton'+trimmed);
                        $('#banButton'+trimmed).attr('id','unbanButton'+trimmed);
                        $form = null;
                        id = null;
                        formIdTrim = null;
                        return;
                      }else {
                         alert("Error");
                      }


                }
             });
         } else {

         }
     });
} 
else if(formIdTrim === "unbanUs") {

    //Stops the submit request
    $(id).submit(function(e){
       e.preventDefault();
    });

    var trimmed = id.substring(9);
    var dataString = $form.serialize();
    var userID = null;
    userID = $("input#unban"+ trimmed).val();

    $("#unbanButton"+ trimmed).click(function(e){
        e.preventDefault();

        //get the form data and then serialize that


        dataString = "userID=" + userID;

        // do the extra stuff here
        if (confirm('Are you sure you want to UNBAN the user with the id of ' + trimmed +'?')) {
            $.ajax({
                type: "POST",
                url: "UserUnban",
                data: dataString,
                dataType: "json",
                success: function(data) {
                    if (data.success) {
                        //$("#banUser"+trimmed).html("");
                        $('#unbanUser'+trimmed).attr('id','banUser'+trimmed);
                        $('#unban'+trimmed).attr('id','ban'+trimmed);
                        $('#unbanButton'+trimmed).attr('value',' Ban User ');
                        $('#unbanButton'+trimmed).attr('name','banButton'+trimmed);
                        $('#unbanButton'+trimmed).attr('id','banButton'+trimmed);
                        $form = null;
                        id = null;
                        formIdTrim = null;
                        return;
                     }else {
                         alert("Error");
                     }


                }
            });
        } else {

        }
    });
}


});

1 个答案:

答案 0 :(得分:0)

尝试:

$("#banButton" + trimmed).off('点击')。on('点击',(功能(e){... ...

我有类似的问题,这是解决方案

相关问题