【发布时间】:2018-02-21 03:07:59
【问题描述】:
我的页面加载时需要加载复选框。它将从 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 的间隔也有效。我很困惑....
【问题讨论】:
-
$("#sample").on("shown.bs.modal", ...是否包含在$(document).ready( funtion(){ ...或$(function() { ...中? -
哦,是的,这就是解决方案!只需放在下面,我将其标记为解决方案。
标签: jquery html css twitter-bootstrap django-templates