Jquery 如何防止多次点击

Jquery 如何防止多次点击,jquery,Jquery,正如下图所示,由于多次单击“保存”按钮,数据库中插入了一个空值。如何防止多次单击,使空数据无法插入数据库 任何帮助都将不胜感激 这是我的剧本 $(document).ready(function(){ $("#save").click(function(){ ajax("save"); }); $("#add_new").click(function(){ $(".entry-form").fadeIn("fast");

正如下图所示,由于多次单击“保存”按钮,数据库中插入了一个空值。如何防止多次单击,使空数据无法插入数据库

任何帮助都将不胜感激

这是我的剧本

$(document).ready(function(){

    $("#save").click(function(){
        ajax("save");
    });

    $("#add_new").click(function(){
        $(".entry-form").fadeIn("fast");    
    });

    $("#close").click(function(){
        $(".entry-form").fadeOut("fast");   
    });

    $("#cancel").click(function(){
        $(".entry-form").fadeOut("fast");   
    });

    $(".del").live("click",function(){
        if(confirm("Do you really want to delete this record ?")){
            ajax("delete",$(this).attr("id"));
        }
    });

    function ajax(action,id){
        if(action =="save")
            data = $("#userinfo").serialize()+"&action="+action;
        else if(action == "delete"){
            data = "action="+action+"&item_id="+id;
        }
        $.ajax({
            type: "POST", 
            url: "ajax.php", 
            data : data,
            dataType: "json",
            success: function(response){
            console.log(JSON.stringify(response))
                if(response.success == "1"){
                    if(action == "save"){
                        $(".entry-form").fadeOut("fast",function(){
                            $(".table-list").append("<tr><td>"+response.cat_name+"</td><td>"+response.cat_code+"</td><td>"+response.letter+"</td><td><a href='#' id='"+response.row_id+"' class='del'>Delete</a></td></tr>");   
                            $(".table-list tr:last").effect("highlight", {
                                color: '#4BADF5'
                            }, 0000);
                        }); 
                        $(".entry-form input[type='text']").each(function(){
                            $(this).val("");
                        });                     
                    }else if(action == "delete"){
                        var row_id = response.item_id;
                        $("a[id='"+row_id+"']").closest("tr").effect("highlight", {
                            color: '#4BADF5'
                        }, 0000);
                        $("a[id='"+row_id+"']").closest("tr").fadeOut();
                    }
                }else{
                    alert("unexpected error occured, Please check your database connection");
                }
            },
            error: function(res){
                alert("Unexpected error! Try again.");
            }
        });
    }
});

验证成功后,取消绑定事件处理程序

$("#save").click(function(){

//Asuming your form validation is success.
 $('#save').unbind( "click" );
//Change the style to indicate it was dissabled.

        ajax("save");
    });

使用返回假;或者使用

在用户单击后禁用按钮

添加禁用onclick…为什么不一次单击就禁用按钮?@senthil-yah它可以工作,但是出现了新问题,当我再次尝试添加新记录时,保存按钮仍然禁用。在添加新记录之前,我需要刷新。您可以使用.on方法$document启用单击处理程序。在“单击”、“保存”时,使用函数{ajaxsave;}@senthil我应该把方法放在哪里?它应该在$add_new.click内