js onclick工作奇怪

时间:2016-01-19 12:34:43

标签: javascript twitter-bootstrap

我有下一个代码

$.ajax({
                url: '/data',
                type: "POST",
                data: JSON.stringify(formData),
                contentType: "application/json",
                dataType: "json",
                success: function(response){
                    for (var i=0; i<response.length; i++) {

                        var htmlEdit = "creating button here";
                        var btnEdit = jQuery(htmlEdit);
                        btnEdit.appendTo(divCollapse);
                        btnEdit.click(function() {
                            editBooking(btnEdit);
                        });
                    }
               }
});


function editBooking(btn) {
    btn.button('loading');
}

所以我有相同项目的行。按钮显示为预期。点击任意按钮可调用最后按钮的加载状态。我做错了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

$.ajax({
    url: '/data',
    type: 'POST',
    data: JSON.stringify(formData),
    contentType: "application/json",
    dataType: "json",
    success: function(response){
        var htmlEdit;
        var btnEdit;
        for (var i=0; i<response.length; i++) {
            htmlEdit = 'creating button here';
            btnEdit = $(htmlEdit);
            btnEdit.appendTo(divCollapse);
            btnEdit.on('click', function(e) {
                editBooking($(this));
            });
        }
   }
});

function editBooking(btn) {
    btn.button('loading');
}