简体   繁体   English

为什么我的 $(document).ready(function()) 没有被执行?

[英]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值相同。

more information about the get function有关获取 function 的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery-为什么在document.ready上调用我的函数时会中断? - jQuery - Why does my function break if called on document.ready? 为什么我不能进入我的$ document Ready函数 - Why can't I get to my $document Ready function 为什么我的Ajax事件发生后或在(document).ready上没有调用我的函数? - Why won't my function get called up after my Ajax event OR on (document).ready? 为什么我的ID容器在Document.ready匿名函数中没有从蓝色更改为红色 - Why does my container with ID not change from Blue to Red in the Document.ready anonymous function 为什么我的jQuery .ready()函数不起作用? - Why does my jQuery .ready() function not work? 为什么 onclick = function() 在文档准备好的内部不起作用? - Why does onclick = function() does not work inside document ready? 为什么我的 $(window).load(function() 在 document.ready 函数中不起作用? - why my $(window).load(function() is not working in document.ready function? 为什么 $(document).ready() function 在我的 Blazor 服务器应用程序中不起作用? - Why is $(document).ready() function not working in my Blazor Server App? 为什么.on无法在我的$(document).ready()函数中使用? - why .on can't be used in my $(document).ready() function? 为什么我的php函数没有被jquery $ .get执行 - why my php function is not executed by jquery $.get
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM