無法在 jQuery AJAX 中多次生成點擊事件 (unable to generate click event more than once in jQuery AJAX)


問題描述

無法在 jQuery AJAX 中多次生成點擊事件 (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');

});

我在 mvc 項目中使用它

通過單擊一行我可以使用 ajax 插入編輯頁面,這沒關係。當使用新記錄按鈕插入新記錄時,我將 div 標籤中的表格列表替換為 Id="addrIndex"

單擊新記錄框上的取消按鈕後,它將返回到表格列表。如果我點擊再次在任何行第二次沒有任何工作。在取消一條新記錄後,如何再次編輯一行。


## 參考解法 #### 方法 1:

delegate your event handler from a parent that's there at the time of binding

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

方法 2:

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 () {

});

方法 3:

bind click on #addrIndex jQuery on

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

(by Ravi HanokmikakunWannaCSharpAnoop)

參考文件

  1. unable to generate click event more than once in jQuery AJAX (CC BY‑SA 3.0/4.0)

#jquery #javascript #ajax






相關問題

讓 jQuery 與 Netscape 7 和 8 一起工作 (Getting jQuery to work with Netscape 7 and 8)

使用 Jquery 的 mvc3 搜索結果 (mvc3 search results with Jquery)

從嵌套的 jquery 函數返回一個值 (Return a value from nested jquery function)

Mencocokkan lebar divisi dengan jquery (Matching division widths with jquery)

無法在 jQuery AJAX 中多次生成點擊事件 (unable to generate click event more than once in jQuery AJAX)

使用雙引號格式並用逗號分隔元素數組 (Implode an element array with double quote format and separated by comma)

選擇不更新 (Select doesn't update)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

AJAX/PHP/JS - 無法將頁面內容加載到容器中 (AJAX/PHP/JS - Cannot load page content into container)

使用 jQuery 將文本插入 textarea (Insert text into textarea with jQuery)

滾動到頁面底部,僅當用戶在 DOM 操作之前已經位於底部時 (Scroll to bottom of page, only if the user already was at the bottom before DOM manipulation)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)







留言討論