同一个jqueryajax请求被发送了好几次

同一个jqueryajax请求被发送了好几次,jquery,ajax,Jquery,Ajax,我已经动态创建了一个表。该表的每一行上都有一个“更新”链接。当我点击该链接时,一切正常,但我在控制台网络(F12)中看到,它生成多个ajax请求,而不是一个(同一个请求被发送多次)。你能检查一下下面的代码并帮我解决这个问题吗 $('#load_table').click(function() { $.get('cont/get_table', function (data) { var item = $('<table>'); $.each

我已经动态创建了一个表。该表的每一行上都有一个“更新”链接。当我点击该链接时,一切正常,但我在控制台网络(F12)中看到,它生成多个ajax请求,而不是一个(同一个请求被发送多次)。你能检查一下下面的代码并帮我解决这个问题吗

$('#load_table').click(function() {
    $.get('cont/get_table', function (data) {

        var item = $('<table>');

        $.each(data.results, function (i, res) {

            var row = $('<tr>').append(
            $('<td class="a">').html(res.id),
            $('<td class="b" contenteditable="true">').html(res.title),
            $('<td class="c" contenteditable="true">').html(res.text),
            $('<td>').html('<a href="#" class="update">Update</a>'));
            item.append(row);
        });

        $("#display").html(item);

        $("body").on("click", ".update", function () {

            var row = $(this).closest('tr'); 
            var id = row.find('td.a').text();
            var title = row.find('td.b').text();
            var text = row.find('td.c').text();

            $.post('cont/update_row', {
                id: id,
                title: title,
                text: text
            }, function (data) {

                //display data
            });
        });
    });
});
$('load#u table')。单击(函数(){
$.get('cont/get_table',函数(数据){
var项目=$('');
$.each(数据、结果、函数(i、res){
变量行=$('')。追加(
$('').html(res.id),
$('').html(res.title),
$('').html(res.text),
$('').html('');
项目.追加(行);
});
$(“#显示”).html(项目);
$(“body”)。在(“单击”,“更新”,函数(){
var行=$(this).closest('tr');
var id=row.find('td.a').text();
var title=row.find('td.b').text();
var text=row.find('td.c').text();
$.post('继续/更新行'{
id:id,
标题:标题,,
文本:文本
},函数(数据){
//显示数据
});
});
});
});

您使用相同的
id='update'
创建了多个
链接,这是不正确的,可能会产生此错误。尝试更改为
class='update'

您使用相同的
id='update'
创建了多个
链接,这是不正确的,可能会产生此错误。尝试更改为
class='update'

基本错误:您有几个id=“update”的元素。换成班级

    $.each(data.results, function (i, res) {

    var row = $('<tr>').append(
    $('<td class="a">').html(res.id),
    $('<td class="b" contenteditable="true">').html(res.title),
    $('<td class="c" contenteditable="true">').html(res.text),
    $('<td>').html('<a href="#" class="update">Update</a>'));
    item.append(row);

    row.on('click', '.update',function(){
    $.post('cont/update_row', {
        id: res.id,
        title: res.title,
        text: res.text
    }, function (data) {

        //display data
    });
});


$("#display").html(item);

基本错误:您有几个id=“update”的元素。换成班级

    $.each(data.results, function (i, res) {

    var row = $('<tr>').append(
    $('<td class="a">').html(res.id),
    $('<td class="b" contenteditable="true">').html(res.title),
    $('<td class="c" contenteditable="true">').html(res.text),
    $('<td>').html('<a href="#" class="update">Update</a>'));
    item.append(row);

    row.on('click', '.update',function(){
    $.post('cont/update_row', {
        id: res.id,
        title: res.title,
        text: res.text
    }, function (data) {

        //display data
    });
});


$("#display").html(item);

谢谢你的回答。实际上,它不适用于“class”,我将“class”改为“id”。现在我把它改回“类”,但问题仍然存在。你能检查一下吗?我想找到你的错误了。移动身体。在ajax调用之外使用('click')函数。每次打电话时,您都会在每个更新按钮中添加另一个事件。谢谢您的回答。实际上,它不适用于“class”,我将“class”改为“id”。现在我把它改回“类”,但问题仍然存在。你能检查一下吗?我想找到你的错误了。移动身体。在ajax调用之外使用('click')函数。每次打电话时,您都会在每个更新按钮中添加另一个事件。谢谢您的回答。实际上,它不适用于“class”,我将“class”改为“id”。现在我把它改回“类”,但问题仍然存在。谢谢你的回答。实际上,它不适用于“class”,我将“class”改为“id”。现在我把它改回“类”,但问题仍然存在。你能检查一下吗。