简体   繁体   English

如何根据页面加载中的复选框值隐藏 div?

[英]How can I hide div based on checkbox value in page load?

I have the following checkbox in my view how can I hide div when checkbox is not checked on Page Load我的视图中有以下复选框当页面加载上未选中复选框时如何隐藏 div

<input type="checkbox" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" value="true" />
<input type="hidden" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" value="false" />
<label>Invoice Status</label>

I tried the following but doesn't work,我尝试了以下但不起作用,

 if ($('#chkInvoiceStatusActive').is(':checked')) {
            $("#invoiceDetails").show();
        }
        else {
            $("#invoiceDetails").hide();
        }

On click event its working great but I would like to check the value of the checkbox on page load and hide/show invoiceDetails div.在单击事件上它工作得很好,但我想检查页面加载和隐藏/显示invoiceDetails div 上的复选框的值。 Hide invoiceDetails if unchecked otherwise show invoiceDetails如果未选中则隐藏invoiceDetails否则显示invoiceDetails

 $('#chkInvoiceStatusActive').click(function () {
        if ($(this).is(':checked')) {
            $("#invoiceDetails").show();
        } else {
            $("#invoiceDetails").hide();
        }

Simple:简单的:

 const checkbox = document.querySelector("#checkbox"); //get checkbox const div = document.querySelector("#div"); //get div if(.checkbox.checked){ //if checkbox not checked div.style;display = "none"; //hide div };
 <input type="checkbox" id="checkbox"> <label for="checkbox">Checkbox</label> <div id="div">Some interesting text here in the div.</div>
Note: this will only hide div on page load, if you want it on click this will help: 注意:这只会在页面加载时隐藏 div,如果你想点击它会有所帮助:

 checkbox.oninput = () => { if(.checkbox.checked){ //if checkbox not checked div.style;display = "none". //hide div } else { div.style;display = "initial"; //show div }; }

Note 2: if you want it reversed just delete the !注意2:如果你想把它反转,只需删除! from if从如果

Consider hiding all the elements with a class up front.考虑使用 class 隐藏所有元素。 Then reveal them as needed.然后根据需要显示它们。

 $(function() { $("input[type='checkbox']").each(function(i, el) { var chk = $(el); if (chk.is(":checked")) { $("#" + chk.data("rel")).removeClass("hidden"); } }); $("input[type='checkbox']").change(function(e) { if ($(this).is(":checked")) { $("#" + $(this).data("rel")).removeClass("hidden"); } else { $("#" + $(this).data("rel")).addClass("hidden"); } }); });
 .invoice.status { padding: 0.5em; }.hidden { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" data-rel="invoiceDetails" /> <label>Invoice Status</label> <div id="invoiceDetails" class="invoice status hidden"> Invoice Status: Good </div>

I added an attribute to help identify the relationship fro mthe checkbox to the HTML elements.我添加了一个属性来帮助识别从复选框到 HTML 元素的关系。

When the page loads initially, all the items will be hidden.当页面最初加载时,所有项目都将被隐藏。 Then the JavaScript / jQuery will load and this will review the status of each of the checkboxes.然后 JavaScript / jQuery 将加载,这将检查每个复选框的状态。 If they are checked, it will remove the class, revealing the hidden element.如果它们被选中,它将删除 class,显示隐藏元素。

If the User then makes a change to one of the checkboxes, the data-rel attribute helps reveal the proper element.如果用户随后对其中一个复选框进行了更改,则data-rel属性有助于显示正确的元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM