简体   繁体   English

Ajax 在引导模式上多次调用 onclick 事件

[英]Ajax call multiple time onclick event on bootstrap modal

By clicking a button Its loaded a bootstrap modal.通过点击一个按钮它加载了一个引导模式。 On the modal there have a form and on click save button I am trying to submit form by ajax call.在模态上有一个表单,点击保存按钮我试图通过ajax调用提交表单。 At first time the ajax call trigger one time, but 2nd time the ajax url trigger two times.第一次ajax调用触发一次,但第二次ajax url触发两次。 I see on firebug console section the post url is called multiple times.我在萤火虫控制台部分看到帖子网址被多次调用。

Here Is my jquery code.这是我的 jquery 代码。

 $(".show-modal").click(function() {
                    $('.upload-modal').modal();

                    $(".save-logo").click(function(e) {

                        e.preventDefault();
                        $.ajax({
                              type : "POST",
                              data : data,                        
                              contentType: false,
                              cache : false,
                              processData: false, 
                              url : "../../io/upload/"
                        }).done(function(rawData) {
                            $('.modal-header .close').click();

                         })

                    });
             })

The problem is that you have your .save-logo click handler inside the .show-modal click handler.问题是您在 .show-modal 单击处理程序中有您的 .save-logo 单击处理程序。 So every time the modal is shown, you attach another click handler to the .save-logo element.因此,每次显示模态时,您都会将另一个单击处理程序附加到 .save-logo 元素。 The code below should fix that problem:下面的代码应该可以解决这个问题:

$(".show-modal").click(function () {
    $('.upload-modal').modal();
});

$(".save-logo").click(function (e) {

    e.preventDefault();
    $.ajax({
        type: "POST",
        data: data,
        contentType: false,
        cache: false,
        processData: false,
        url: "../../io/upload/"
    }).done(function (rawData) {
        $('.modal-header .close').click();

    })

});
$(function(){
var editUserId;
$('#edit-user-modal').on('show.bs.modal', function(e) {
    editUserId = $(e.relatedTarget).data('userid');
});
$("#edit-user-form").submit(function(event) {
 
    event.preventDefault();
    var form = $(this);
    var route = "/edit-user/" + editUserId;

    if(validateEditUserForm()){
        $.ajax({
        type: "POST",
        url: route,
        data: form.serialize(), 
        success: function(Response)
        { 
            if(Response){
                console.log(Response.status);
                console.log(Response.message);
                if(Response.status == 'success'){
                    $('#adduser-alert-box').hide();
                    $("#add-user-form")[0].reset();
                    $('#adduser-success-box').show();
                    $('#adduser-success-box').html(Response.message);
                }
                else{
                    console.log('User could not be added');
                    $('#adduser-alert-box').show();
                    $('#adduser-alert-box').html(Response.message);
                }
            }
            else{
                console.log('No Response');
                $('#adduser-alert-box').show();
                $('#adduser-alert-box').html('No Response');
            }
        }
        });
    }
    else{
        console.log('Validation Error');
        $('#adduser-alert-box').show();
        $('#adduser-alert-box').html('Please Fill All the Fields Correctly');
    }


});});

I faced the same issue and randomly i tried something .我遇到了同样的问题,随机尝试了一些东西。 so if you create a global variable to store the 'id' you pass from your button then store the id from the 'data' attribute of your button then it won't make multiple requests when clicked since you have declared the variable globally!因此,如果您创建一个全局变量来存储您从按钮传递的 'id' 然后存储来自按钮的 'data' 属性的 id,那么它不会在单击时发出多个请求,因为您已经全局声明了该变量!

Hope it helps.希望能帮助到你。

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

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