根据加载的复选框状态隐藏或显示DIV

发布于 2025-02-12 13:09:06 字数 237 浏览 1 评论 0原文

这个问题与以下问题有关:

hide或基于复选框状态显示DIV

如何检查复选框是否已在负载上检查。

我正在使用ASP-For加载内容。

This question is related to:

Hide or show div based on checkBox status

How to check if the checkbox is already checked on load.

I am using asp-for to load the content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2025-02-19 13:09:07

要检查是否已经在Load.tra.try上检查了复选框

如果

<input type="checkbox" asp-for="gridCheck1" />
<div id="ShowHideMe">
    <p>some content</p>
</div>

$(function () {
      myFunction();
 )};
 $('#gridCheck1').change(function () {
      myFunction();
 });
 function myFunction() {
     const div = document.getElementById('ShowHideMe');
     if ($("#gridCheck1").prop('checked')) {
          div.style.display = "block";
     } else {
          div.style.display = "none";
     }
 }

If you want to check if the checkbox is already checked on load.Try to use $(function(){}):

html:

<input type="checkbox" asp-for="gridCheck1" />
<div id="ShowHideMe">
    <p>some content</p>
</div>

js:

$(function () {
      myFunction();
 )};
 $('#gridCheck1').change(function () {
      myFunction();
 });
 function myFunction() {
     const div = document.getElementById('ShowHideMe');
     if ($("#gridCheck1").prop('checked')) {
          div.style.display = "block";
     } else {
          div.style.display = "none";
     }
 }
泪是无色的血 2025-02-19 13:09:07

我如何解决这个问题,希望它能有所帮助:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script>
    $(function() {
        $('#gridCheck1').change(function() {

            myFunction();
        });
    });
    $(document).ready(function() {
        myFunction();
    });

    function myFunction() {
        $('#ShowHideMe').toggle($('#gridCheck1').is(':checked'));
    }
</script>

<div class="form-group row">
    <div class="col-sm-2">Checkbox</div>
    <div class="col-sm-10">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" id="gridCheck1">
            <label class="form-check-label" for="gridCheck1">
                Example checkbox
            </label>
        </div>
    </div>
</div>

<div id="ShowHideMe" style="display:none">
    <p>some content</p>
</div>

How i solved this, hope it helps :

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script>
    $(function() {
        $('#gridCheck1').change(function() {

            myFunction();
        });
    });
    $(document).ready(function() {
        myFunction();
    });

    function myFunction() {
        $('#ShowHideMe').toggle($('#gridCheck1').is(':checked'));
    }
</script>

<div class="form-group row">
    <div class="col-sm-2">Checkbox</div>
    <div class="col-sm-10">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" id="gridCheck1">
            <label class="form-check-label" for="gridCheck1">
                Example checkbox
            </label>
        </div>
    </div>
</div>

<div id="ShowHideMe" style="display:none">
    <p>some content</p>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文