添加&动态删除 HTML DIV

发布于 2024-10-16 11:37:19 字数 1029 浏览 1 评论 0原文

我在动态添加和删除 html div 时遇到问题。下面是我正在处理的简化代码:

<input type="button" value="+ Add More Division" name="add_div" id="add_div">

<div id="div_1">
    <input type="button" value="+ Delete Div A" name="del_a" id="del_a">
</div>

<div id="div_2">
    <input type="button" value="+ Delete Div B" name="del_b" id="del_b">
</div>

<div id="div_3">
    <input type="button" value="+ Delete Div C" name="del_c" id="del_c">
</div>

这是情况,div_1、div_2 和 div_3 可以动态添加和删除。 div_1 将始终与按钮 ID del_a 关联,div_2 与 del_b 关联,div_3 与 del_c 关联。最多只能添加 3 个 div。

我需要可以添加和删除 div 并重用 div_1、div_2 和 div_3 以及关联的按钮 id 的 jquery。

例如,如果用户删除了div_2,并且用户想要添加更多分区(只能再添加1个),jquery将尝试查找现有的div,并以某种方式记住它。由于 div_2 不存在,因此将添加 div_2,与顺序无关。 div_3 可以先放在其他 div 之前或之后。

我只是想给用户编辑表单的自由,因为这个庞大的表单。我不知道如何使用 .each() 在 jQuery 中执行此操作。

感谢您的帮助。

编辑: 默认情况下,表单上仅存在 div_1。用户可以自由添加另外 2 个 div 以添加更多 div。当 div_2 或 div_3 存在时,用户还可以删除 div_1。必须存在一个 div。

I have a problem to add and delete html div dynamically. Below is the simplified code that I am working on:

<input type="button" value="+ Add More Division" name="add_div" id="add_div">

<div id="div_1">
    <input type="button" value="+ Delete Div A" name="del_a" id="del_a">
</div>

<div id="div_2">
    <input type="button" value="+ Delete Div B" name="del_b" id="del_b">
</div>

<div id="div_3">
    <input type="button" value="+ Delete Div C" name="del_c" id="del_c">
</div>

This is the situation, div_1, div_2 and div_3 can be added and removed dynamically. div_1 will always be associated with button id del_a, div_2 with del_b and div_3 with del_c. It is only 3 divs maximum can be added.

I need jquery that can add and remove the div and reuse div_1, div_2 and div_3 along with the associated button ids.

For example, if the user delete the div_2, and the user want to add more division (which is only 1 more can be added), the jquery will try to find the existing divs, and somehow remember it. Since div_2 is not exist, div_2 will be added, does not matter the order. div_3 can go first before or after the other divs.

I just want to give freedom for the user to edit the form, since this massive form. I do not know how to do this in jQuery using .each().

Thanks for your help.

Edited:
By default, only div_1 is exist on the form. The user can have the freedom to add another 2 divs to add more divs. The user is also have the capability to delete div_1 when either div_2 or div_3 exist. One div must be exist.

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

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

发布评论

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

评论(4

预谋 2024-10-23 11:37:19

你可以使用 detach() 函数...它会简单地删除该 div,如果你想再次添加它,请提供该 div 的引用并再次添加它..像这样

假设,用户像这样删除了第一个 div......

var div

$('#del_a').click(function(){
div= $('#div_1').detach();
});

用户单击添加按钮时,您可以执行类似的操作...

$('#add_div').click(function(){
$(div).appendTo('where you want to append')
});

我认为这可能对您有帮助..

you can use detach() function....it will simply remove the div and if you want to add it again have, a reference of that div and add it again..something like this

suppose, user deleted the first div like this...

var div

$('#del_a').click(function(){
div= $('#div_1').detach();
});

...

and when user click on add button you can do something like this...

$('#add_div').click(function(){
$(div).appendTo('where you want to append')
});

i think this might help you..

独享拥抱 2024-10-23 11:37:19
If you want to delete div dynamically den use it:
$('#mydiv').empty();

使用 jQuery 添加/删除动态 HTML 元素,请参见下文
使用 jQuery 动态添加/删除 HTML 元素

If you want to delete div dynamically den use it:
$('#mydiv').empty();

Add /Remove dynamically HTML element with jQuery plz see below
Add /Remove dynamically HTML element with jQuery

独享拥抱 2024-10-23 11:37:19

您可以使用三个变量作为当前 div 的标志。单击添加 div 按钮时,检查第一个未设置的标志,添加该 div 并为该特定 div 设置标志。如果设置了所有三个标志,则禁用添加更多 div 按钮。希望这有帮助。

You can have three variables acting as flags for the divs present. When the add div button is clicked, check for the first flag which is not set, add that div and set the flag for that particular div. If all three flags are set, then disable the add more divs button. Hope this helps.

如若梦似彩虹 2024-10-23 11:37:19

它工作正常(请优化此代码):)

<script type="text/javascript" >
$(document).ready(function(){
$('#add_div').click(function(){
    if(($('#del_a').is(":hidden"))){$("#del_a").show(); return; }
    if(($('#del_b').is(":hidden"))){$("#del_b").show(); return; }
    if(($('#del_c').is(":hidden"))){$("#del_c").show(); return; }
});
$('#del_a').click(function(){
    if($('#del_b').is(":hidden") && $('#del_c').is(":hidden") )
        return;
    $('#del_a').hide();
});
$('#del_b').click(function(){
    if($('#del_a').is(":hidden") && $('#del_c').is(":hidden") )
        return;
    $('#del_b').hide();
});
$('#del_c').click(function(){
    if($('#del_a').is(":hidden") && $('#del_b').is(":hidden") )
            return;
    $('#del_c').hide();
});

});

Its working fine (Plz optimized this code) :)

<script type="text/javascript" >
$(document).ready(function(){
$('#add_div').click(function(){
    if(($('#del_a').is(":hidden"))){$("#del_a").show(); return; }
    if(($('#del_b').is(":hidden"))){$("#del_b").show(); return; }
    if(($('#del_c').is(":hidden"))){$("#del_c").show(); return; }
});
$('#del_a').click(function(){
    if($('#del_b').is(":hidden") && $('#del_c').is(":hidden") )
        return;
    $('#del_a').hide();
});
$('#del_b').click(function(){
    if($('#del_a').is(":hidden") && $('#del_c').is(":hidden") )
        return;
    $('#del_b').hide();
});
$('#del_c').click(function(){
    if($('#del_a').is(":hidden") && $('#del_b').is(":hidden") )
            return;
    $('#del_c').hide();
});

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