[英]Why does my $(document).ready(function()) not get executed?
I have a checkbox on my html view which looks like that:我的 html 视图上有一个复选框,如下所示:
<input type="checkbox" id="hasPrereqBlock" name="hasPrereqBlock" onchange="hasPrereqBlockHandler(this)">
And the function that gets triggered by that, looks like that:由它触发的 function 看起来像这样:
function hasPrereqBlockHandler(cb){
if (cb.checked){
$("#campaignPrereqBlockRevDiv").show();
$("#instruction_1_RevDiv_M").hide();
$("#instruction_2_RevDiv").show();
} else {
$("#campaignPrereqBlockRevDiv").hide();
$("#instruction_1_RevDiv_M").show();
$("#instruction_2_RevDiv").hide();
}
}
When i load the page i want to execute this function and give it a reference to the checkbox, so it displays only the wanted stuff, due to the status of the checkbox, so i have this function:当我加载页面时,我想执行这个 function 并给它一个对复选框的引用,所以它只显示想要的东西,由于复选框的状态,所以我有这个 function:
$(document).ready(function() {
hasPrereqBlockHandler($("#hasPrereqBlock"));
});
I also tried it with document.getElementById("hasPrereqBlock") instead of $("#hasPrereqBlock"), but every of thos 3 elements are shown and they are only hidden when i click the checkbox.我还尝试使用 document.getElementById("hasPrereqBlock") 而不是 $("#hasPrereqBlock"),但是这 3 个元素中的每一个都会显示,并且仅当我单击复选框时它们才会隐藏。 Why does my code not work?
为什么我的代码不起作用?
function hasPrereqBlockHandler(cb) { if (cb.checked) { $("#campaignPrereqBlockRevDiv").show(); $("#instruction_1_RevDiv_M").hide(); $("#instruction_2_RevDiv").show(); } else { $("#campaignPrereqBlockRevDiv").hide(); $("#instruction_1_RevDiv_M").show(); $("#instruction_2_RevDiv").hide(); } } $(document).ready(function() { console.log("document ready"); hasPrereqBlockHandler($("#hasPrereqBlock")); });
#campaignPrereqBlockRevDiv, #instruction_2_RevDiv { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" id="hasPrereqBlock" name="hasPrereqBlock" onchange="hasPrereqBlockHandler(this)"> <div id="instruction_1_RevDiv_M">Review 1</div> <br> <div id="instruction_2_RevDiv">Review 2</div> <br> <div id="campaignPrereqBlockRevDiv">Campaing</div>
Your jQuery object has no .checked
.您的 jQuery object 没有
.checked
。 If you console.log
cb.checked
it returns undefined
instead of a boolean, so you know there is something wrong.如果你
console.log
cb.checked
它返回undefined
而不是 boolean,所以你知道有问题。 .checked
doesn't exists on a jQuery checkbox object. .checked
在 jQuery 复选框 object 上不存在。
Change:改变:
cb.checked
Into:进入:
cb.prop('checked')
You're mixing JavaScript and jQuery, As dcangulo stated: change the element you're sending to the function in the document ready part:您正在混合 JavaScript 和 jQuery,正如 dcangulo 所述:将您发送的元素更改为文档准备部分中的 function:
$(document).ready(function() {
hasPrereqBlockHandler($("#hasPrereqBlock").get(0));
});
get
returns the element at the given index, and since there is one (you're using an ID, so I assume there is just one) it sends the first. get
返回给定索引处的元素,并且由于有一个(您使用的是 ID,所以我假设只有一个)它发送第一个。 The element sent to the function is the same as the this
value that gets send when you check the input.发送到 function 的元素与检查输入时发送的
this
值相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.