代码应该说明一切。我对两个复选框都使用了相同的更新功能,但是模态外的一个成功打印了它是否被检查但是模态中的那个没有。知道为什么吗?
$("#nonModalCheckbox").on("click", update);
$("#modalCheckbox").on("click", update);
$("#modal_show").on("click", showModal);
function showModal() {
$("#myModal").modal("show");
}
function update() {
console.log($(this).prop("checked"))
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input type="checkbox" id="nonModalCheckbox">test
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×
</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body" id="modal_body">
<div class="checkbox" id="modalCheckbox"><label><input type="checkbox">modal test</label></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="apply">Apply</button>
</div>
</div>
</div>
</div>
<button id="modal_show">Show Modal!</button>
&#13;
答案 0 :(得分:3)
您已在模式中将ID modalCheckbox
分配给div而不是checkbox
。以下将有效。
<input type="checkbox" id="modalCheckbox">modal test</label>
$(document).ready(function () {
$("#nonModalCheckbox").on("click", update);
$("#modalCheckbox").on("click", update);
$("#modal_show").on("click", showModal);
function showModal() {
$("#myModal").modal("show");
}
function update() {
console.log($(this).prop("checked"))
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input type="checkbox" id="nonModalCheckbox">test
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×
</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body" id="modal_body">
<div class="checkbox">
<label>
<input type="checkbox" id="modalCheckbox">modal test</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="apply">Apply</button>
</div>
</div>
</div>
</div>
<button id="modal_show">Show Modal!</button>
&#13;