检查 div html 中不包含

发布于 2024-08-12 18:40:07 字数 284 浏览 2 评论 0原文

我有一个 div

和一些复选框,用于在该 maindiv 内添加一些 html 元素。

当我选中该复选框时,我需要在检查它不包含该元素后附加

。细分 ID 将根据所选复选框的不同而变化。

当我取消选中该复选框时,我需要删除

(如果存在)。

我怎样才能使用 jquery 做到这一点?

I have a div <div id="maindiv"></div> and some checkboxes to add some html element inside that maindiv.

When i check that checkbox, i need to append <div id="subdiv"></div> after checking that it doesn't contains that element. The subdiv id will vary according to the checkbox selected.

When i uncheck the checkbox, i need to remove the <div id="subdiv"></div> if exists.

How can i do it using jquery?

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

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

发布评论

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

评论(4

一笔一画续写前缘 2024-08-19 18:40:07

试试这个:

    $(function() {
        var chk = "<input id=\"sex\" name=\"sex\" type=\"checkbox\">";
        var sub = "<div id=\"subdiv\">hello</div>";
        $("#container").prepend(chk);

        $("#sex").click(function(event) {
            if ($("#maindiv").data('sex')) {
                $("#maindiv").data('sex', false);

                $("#subdiv").remove();
            } else {
                $("#maindiv").data('sex', true);
                $("#maindiv").append(sub);
            }
        });             
    });

HTML:

<div id="container">
    <div id="maindiv">main</div>
</div>

我使用数据来进行检查。

Try this:

    $(function() {
        var chk = "<input id=\"sex\" name=\"sex\" type=\"checkbox\">";
        var sub = "<div id=\"subdiv\">hello</div>";
        $("#container").prepend(chk);

        $("#sex").click(function(event) {
            if ($("#maindiv").data('sex')) {
                $("#maindiv").data('sex', false);

                $("#subdiv").remove();
            } else {
                $("#maindiv").data('sex', true);
                $("#maindiv").append(sub);
            }
        });             
    });

HTML:

<div id="container">
    <div id="maindiv">main</div>
</div>

I used data to do the checking.

乖不如嘢 2024-08-19 18:40:07

为什么不在 HTML 中添加

并在需要时隐藏/显示此 div?

Why don't you add <div id="subdiv"></div> in your HTML and just hide/show this div when you need it?

孤独陪着我 2024-08-19 18:40:07
function myToggle() {
  if($("#mycheck:checked").val()!=null) {
    //check is true
    $("#maindiv").append("<div id='subdiv'></div>");
  }
  else {
    //check is false
    $("#subdiv").remove();
  }
}
function myToggle() {
  if($("#mycheck:checked").val()!=null) {
    //check is true
    $("#maindiv").append("<div id='subdiv'></div>");
  }
  else {
    //check is false
    $("#subdiv").remove();
  }
}
那小子欠揍 2024-08-19 18:40:07
$('#my-checkbox').change(function() {
  if ($(this).is(':checked')) {
    if (!$('#subdiv').length) {
      $('#maindiv').append('<div id="subdiv">...</div>');
    }
  } else {
    $('#subdiv').remove();
  }
});
$('#my-checkbox').change(function() {
  if ($(this).is(':checked')) {
    if (!$('#subdiv').length) {
      $('#maindiv').append('<div id="subdiv">...</div>');
    }
  } else {
    $('#subdiv').remove();
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文