我的页面加载时需要加载复选框。它将从 DjangoTemplate 提供的字典中查找并选中一些复选框。
我尝试使用 "shown.bs.modal"
它执行代码,但后来我发现我的复选框设置回默认未选中状态。
我发现使用 setInterval()
来设置复选框可以达到我想要的效果。模态加载后似乎需要 1 - 2 秒才能实际选中复选框。所以,在 "shown.bs.modal"
之后肯定会发生一些事情,将我的复选框重置为未选中状态。
到目前为止,我的解决方案是在 "shown.bs.modal"
之后使用 setTimeout()
来选中复选框。但我仍然想知道是什么原因。
/*
Used to detect if modal page button is triggered or not. These codes does
not set the check boxes checked.
*/
$("#sample").on("shown.bs.modal", function(event) {
for (var i = 0; i < index_checkbox.length; i ++) {
console.log("something is actually happened here");
$("#checkbox-" + index_checkbox).prop("checked", true);
console.log("the value of `prop('checked')` is changed");
console.log("but later it goes back to `false`");
}
}).modal("show");
// These codes checked the check boxes after 1 - 2 seconds.
window.setInterval(function () {
for (var i = 0; i < index_checkbox.length; i ++) {
$("#checkbox-" + index_checkbox).prop("checked", true);
}
}, 1000);
// The checkbox generated from DjangoTemplate.
<input id="checkbox-{{ teacher.id }}" type="checkbox" name="teacher"
value="{{ teacher.id }}"
ng-model="teacher_checkbox[{{ forloop.counter0 }}]" />
编辑:实际上,将 setInterval()
设置为 100
毫秒也可以。但如果可能的话,我不想使用 setInterval()
。
编辑:0
的间隔也有效。我很困惑....
最佳答案
您必须将任何 Bootstrap 事件包装起来,例如 'shown.bs.whatever'
中的任何一个
$(document).ready( function()
});
或
$(function(){
});
关于jquery - Bootstrap 模态 "shown.bs.modal"不会改变我在模态内的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46180014/