我有一个引导模式表单,其中包含要跟踪的项目列表。如果选中了“所有”框,则会选择/取消选择列表中的所有项。jquery函数在每次显示我不理解的模式时都会被调用一次。第一次打开模型时,调用jquery函数。第二次打开模型,jquery函数会被调用两次。第三次打开模型,调用jquery函数三次。诸若此类。为什么每次打开该模式时都会调用此复选框函数一次?jquery是否将事物推到某个类型的堆栈中?如果用户选择要检查的所有项目,我想要做的是弹出一个警报。但我不想让他/她得到多个警报。代码如下:
$('#realTimeModal').on('show.bs.modal', function (e) {
var modal = $(this)
console.log('controller-1253: realTimeModal handler');
//first, clear out any previous layers
clearLayers();
console.log('show_realtime, updatePositions: ', updatePositions );
clearInterval(updatePositions);
/*
* Check to see if the 'Toggle All' checkbox is checked. If so
* either select all checkboxes or deselect them.
*/
$("#check_rt").change(function () {
console.log('==================================');
console.log('===== in #check_rt =========');
console.log('==================================');
var checked = $(this).prop('checked');
if (checked == true) {
$.each($("input[class='form-check-input callSign_checkbox-rt']"), function () {
$(this).prop('checked', true);
});
trackAll = 'true';
}
else {
$.each($("input[class='form-check-input callSign_checkbox-rt']"), function () {
$(this).prop('checked', false);
});
trackAll = 'false';
}
//if ( trackAll == 'true' )
//alert('If you select all aircraft, breadcrumbs will not be displayed.');
});
}) // $('#realTimeModal').on('show.....)
我想要做的是,只要用户选中“选择所有”复选框,就会弹出警报。但我只想要一个警报,而不像打开模式的次数那么多。我是不是遗漏了jquery的一些基本内容?
谢谢.
发布于 2019-01-05 17:51:35
每次打开模式时都要添加一个事件处理程序。
它的功能-
$("#check_rt").change(function () { ... }
定义为-
$('#realTimeModal').on('show.bs.modal', function (e) { ... }
情况不应如此。您应该在模态显示函数调用之外调用$("#check_rt").change()
。
https://stackoverflow.com/questions/54054466
复制相似问题