我的 codeigniter 项目中的事件触发有问题。我想在我的页面中动态加载一个表。该表是通过 ajax 调用从我的 Controller 返回的。我想在该表的跨度中触发点击事件。我尝试了很多方法,但没有用。我在这里附上我的代码。请帮助我找到解决方案。
Controller :
$str = '<table>
<tr>
<td class="cost date" id="date_'.$request['SupplyID'].'">
<span class="editable" style="display: none;">
<input type="text" class="expiry" autocomplete="off" data-date-id="date_'.$request['SMSSupplyID'].'">
</span>
<span class="cnt" style="display: inline;">Choose</span>
</td>
</tr>
</table>';
echo $str;
脚本在 View 中:
function loadmore()
{
var url;
url = '/SMS/SMS/loadMoreRequests';
$.ajax({
type: 'post',
url: url,
data: {
limit:val
},
success: function (response) { console.log(response);
$("#requestListTable").html(response);
val +=10;
}
});
}
$('.date').find('.cnt').on('click', function(){
console.log(1); // for testing
});
我尝试了以下更改,但没有用
1)
$('.date').find('.cnt').live('click', function(){
console.log(1); // for testing
});
2)
$('.date').find('.cnt').click(function(){
console.log(1); // for testing
});
最佳答案
$("container for the table").on("click", ".cnt", function() {
});
您可以为此使用事件委托(delegate)。将事件绑定(bind)到文档准备就绪时出现在 dom 上的父元素。或者你可以简单地使用,
$("body").on("click", ".cnt", function() {
});
关于javascript - 在 ajax 响应元素上触发 JQuery 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46803143/