通过Ajax调用加载元素后,单击事件不工作

On Click event not working after loading element through Ajax call

本文关键字:单击 事件 工作 Ajax 调用 加载 元素 通过      更新时间:2023-09-26

我正在加载页面(包含javascript),然后我正在加载PartialView(包含表)。问题是,我不能选择表id或onClick事件不触发。

Index.cshtml

$('#sortingRow').on('click', 'th a', function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        data: { sortOrder: sortOrder },
        success: function (result) {
            $('#sortingRow').parent().html(result);
            LoadSampleDesign();
        }
    });
    return false;
});

_PartialView.cshtml

<table>
    <tr id="sortingRow">
        <th>
            <a href="link">Click Me</a>
        </th>
    </tr>
</table>

partialView正在使用Ajax加载,如下所示:

$(sections).on('click', '#sectionPageLinks a', function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        success: function (result) {
            $('#sectionPageLinks').parent().html(result);
            LoadSampleDesign();
        }
    });
    return false;
});

我试过JsFiddle和javascript似乎工作正常。此外,起初,你可能认为问题是,jquery找不到表,因为它是加载DOM完成后,但这就是为什么我使用"on()",不使它活,所以它应该能够找到#表,即使在DOM完成后。

通过Ajax加载部分视图后,click事件不再被绑定。按如下方式更改行单击事件。

$('#sortingRow').on('click', 'th a', function () {

$(document).on('click', '#sortingRow th a', function () {