简体   繁体   English

jQuery 多个 forms 在侧边栏中具有相同的提交

[英]jQuery multiple forms in sidebar with same submit

I have a table with some records in it and each of that records can be edited.我有一张表,里面有一些记录,每条记录都可以编辑。 I have a sidebar with edit form in it with the same inputs and the same submit button.我有一个带有编辑表单的侧边栏,其中包含相同的输入和相同的提交按钮。 When I try to execute the function on that button which will send an AJAX request, it executes as many times as many sidebars I opened before, when I need to update only that record which was actually edited.当我尝试在该按钮上执行 function 将发送 AJAX 请求时,它执行的次数与我之前打开的边栏一样多,而我只需要更新实际编辑的记录。

That's the code:那是代码:

    // On Edit
    $('.action-edit').on("click",function(e){
        e.stopPropagation();
        $(".add-new-data").addClass("show");
        $(".overlay-bg").addClass("show");

        const row = $(this).closest('td').parent('tr').first();
        const agendaID = row.data('agenda_id');
        const form = document.querySelector('#update_form');
        const url = $(form).data('action_url').replace('.ID.', agendaID);

        getAgendaInfo(agendaID);

        $('.add-data-btn').on('click', function (e){
            e.preventDefault();
           
            console.log(agendaID); // displays IDs of all records where sidebar was opened. 

            const ajaxData = grabFormData();

            editAgenda(url, row, ajaxData);
        })

    });

I think the issue is that you are adding new click event listener each time you show a new editor but you don't remove the previous event listener.我认为问题在于您每次显示新编辑器时都添加了新的click事件侦听器,但您没有删除以前的事件侦听器。

Try doing $('.add-data-btn').off() before the $('.add-data-btn').on('click'... .尝试在 $('.add $('.add-data-btn').on('click'...之前执行$('.add-data-btn').off()

The problem you have is that your click handler is created whenever that .on() function is evaluated.您遇到的问题是,每当评估.on() function 时,都会创建您的点击处理程序。 The issue is that you execute that multiple times.问题是您多次执行该操作。 Instead of that, you will need to perform a few steps for an elegant solution:取而代之的是,您需要执行几个步骤以获得优雅的解决方案:

#1 #1

Find a tag which exists before you populate your action-edit elements.在填充action-edit元素之前找到一个存在的标签。 In the worst case that would be body and I suggest that you need to start experimenting with在最坏的情况下,这将是body ,我建议你需要开始尝试

$(function() {
    $('body').on("click", '.action-edit',function(e){
        e.stopPropagation();
        $(".add-new-data").addClass("show");
        $(".overlay-bg").addClass("show");

        const row = $(this).closest('td').parent('tr').first();
        const agendaID = row.data('agenda_id');
        const form = document.querySelector('#update_form');
        const url = $(form).data('action_url').replace('.ID.', agendaID);

        getAgendaInfo(agendaID);

        $('.add-data-btn').on('click', function (e){
            e.preventDefault();
           
            console.log(agendaID); // displays IDs of all records where sidebar was opened. 

            const ajaxData = grabFormData();

            editAgenda(url, row, ajaxData);
        })

    });
});

Note that this needs to run exactly once and will automatically create click handlers for you.请注意,这需要只运行一次,并且会自动为您创建click处理程序。

#2 #2

Find the closest tag to your grid which already exists at the time the page is loaded and change the selector accordingly.找到在页面加载时已经存在的最接近您的网格的标签,并相应地更改选择器。

#3 #3

Test, test, test测试,测试,测试

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

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