简体   繁体   中英

unable to generate click event more than once in jQuery AJAX

   $('table tbody tr').click(function add_div() {
    if ($('#dynEdit').length > 0) {
        $('#dynEdit').remove();
        return false;
    }
    $(this).after('<div id="dynEdit"></div>');
    $.ajax(
        {
            url: '/TransJobAddress/EditAddress',
            datatype:'html',
            success: function(data,textStatus,jqXHR)
            {
                $('#dynEdit').html(data);
            },
            error:function( jqXHR, textStatus,errorThrown)
            {
                alert('The server saying:' + errorThrown);

            }
        });

});
$('#close').click(function closediv() {
    $('#addrIndex').load('/TransJobAddress/ListAddresses #addrIndex table');

});

I am using this in mvc project

By clicking on a row i can insert edit page using ajax this is ok. When Insert new record using new record button I am replacing table list which is in div tag with Id="addrIndex"

After clicking CANCEL button on new record box it is going back to table list .If I click again on any row second time nothing is working. How can I Edit a row again more that one time after cancel a new record also.

从绑定时的父级委派您的事件处理程序

$(document.body).on('click','tr', function(e) {//...});

The problem is when the HTML gets replaced, the elements lose its bindings. Try binding the click in your div.

$("#addrIndex").on("click", "table tbody tr", function () {

});

bind click on #addrIndex jQuery on

$("#addrIndex").on('click', 'table tbody tr', function(){
   ///your code here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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