jquery - Bootstrap 模态 "shown.bs.modal"不会改变我在模态内的复选框

标签 jquery html css twitter-bootstrap django-templates

我的页面加载时需要加载复选框。它将从 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/

相关文章:

jquery - 需要从 css 转换的间隔

javascript - 将 stackblur 与 Canvas 一起使用不会模糊我的图像

html - ul li 列表样式导航,一些元素右对齐

html - 在导航菜单中选择当前或事件链接的语义方式

jQuery 事件链接当前页面

javascript - jQuery 行在回调函数内不起作用

javascript - jquery 上的 Woocommerce 单选按钮检查

javascript - 使用javascript绘制动画饼图

加载外部 Jar(插件架构)时 JavaFX 样式丢失

javascript - 隐藏侧边栏 - marginRight/display CSS bug with Javascript