[英]On Click event not working after loading element through Ajax call
我正在加载页面(包含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完成之后也应该能够找到#table。
通过Ajax加载部分视图后,不再绑定click事件。 如下更改行单击事件。
$('#sortingRow').on('click', 'th a', function () {
$(document).on('click', '#sortingRow th a', function () {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.