我有使用ag-grid和一些自定义javascript的页面来更改按钮属性“禁用”。当第一次加载页面时脚本不能正常工作,但是如果我刷新页面它运行正常。
什么可能导致这个问题?这是js代码
$(document).ready(function(){
$('.table.data tbody').on('change', '.list-checkbox input[type="checkbox"]', function(){
var repCount = $('.table.data tr.republishable input[type="checkbox"]:checked').length;
var notRepCount = $('.table.data tr:not(.republishable) input[type="checkbox"]:checked').length;
$("#btn-republish").prop("disabled", repCount === 0 || notRepCount > 0)
})
});
看来你有我们称之为“竞争条件”的东西。
您可能在包含jQuery的行上有async
(类似于<script src="/path/to/jQuery" async>
,我认为它是基于语法的jQuery)。
只需从此行中删除async
即可使代码可靠地运行。
解决方案是jQuery选择器'.table.data tbody'不够好。它太低了,只要网格中有很多数据,所以在我发现条目表数据发生变化之前。所以改变css选择器有帮助。