按钮单击事件多次触发

时间:2017-11-02 19:53:11

标签: javascript

所以这个按钮多次触发有问题。如果我点击一个按钮它工作正常(只发射一次),但如果我点击另一个按钮后,它会发射两次,依此类推。我尝试了.of(“click”),$(this).unbind()和.one(“click”,function(){})方法,但它们没有用。请帮帮我,我真的需要帮助。提前谢谢!

   $("#submitMember").on("click",function(){

    alert("Member submited!");

    //pull the data from the inputs
    var firstName = $("#firstName").val();
    var lastName = $("#memberName").val();
    var email = $("#memberEmail").val();
    var adeziune = $("#adeziune").val();
    var address = $("#memberAddress").val();
    var job = $("#memberJob").val();
    var jobPlace = $("#memberJobPlace").val();
    var memberCNP = $("#memberCNP").val();

    //checking if all the inputs had benn filled
    if(firstName=="" || lastName=="" || email=="" || adeziune=="" || address=="" || job=="" || jobPlace=="" || memberCNP == "") {
        alert("Va rugam sa completati toate campurile !");
    } else {

        //send the request to members_script.php
        $.ajax({
            url: '../php_scripts/members_script.php',
            method: 'POST',
            data: {
                memberSubmited: 1,
                firstName: firstName,
                lastName: lastName,
                email: email,
                adeziune: adeziune,
                address: address,
                job:job,
                job_place: jobPlace,
                memberCNP: memberCNP
            },
            success: function(response) {
                console.log(response);
                $("#memberTableBody").html(response);
            },
            dataType: 'text'
        });
    }
    $("button").unbind();
});

//submiting  the post
$("#submitPost").on('click', function(event){
    //preventing the form submision
    event.preventDefault();
    alert("Post submited!");

    var imageName = $("#postImage").val();
    var postTitle = $("#postTitle").val();
    var postAuthor = $("#postAuthor").val();
    var postTags = $("#postTags").val();
    var postCategory = $("#selectCategory").val();
    var postContent = $("#postContent").val();

    var image = imageName.replace(/^.*\\/, "");

    //chcking if the fields are filled
    if(postTitle == "" || postAuthor == "" || postTags == "" || postCategory == "" || imageName == "" || postContent == "") {
        alert("Va rugam sa completati toate campurile!");
    } else {

         $.ajax({
            url: '../php_scripts/post_script.php',
            method:'POST',
            data: {
                postSubmited: 1,
                postTitle: postTitle,
                postAuthor: postAuthor,
                postTags: postTags,
                postImage: image,
                postCategory: postCategory,
                postContent: postContent,
            },
            success: function(response){
                $("#postsTableBody").html(response);
                console.log(response);
            },
            dataType: 'text'
        });
    }
    $("button").unbind();
    alert("Unbinded");
});

0 个答案:

没有答案
相关问题