添加&动态删除 HTML DIV
我在动态添加和删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以使用 detach() 函数...它会简单地删除该 div,如果你想再次添加它,请提供该 div 的引用并再次添加它..像这样
假设,用户像这样删除了第一个 div......
当
用户单击添加按钮时,您可以执行类似的操作...
我认为这可能对您有帮助..
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 thissuppose, user deleted the first div like this...
...
and when user click on add button you can do something like this...
i think this might help you..
使用 jQuery 添加/删除动态 HTML 元素,请参见下文
使用 jQuery 动态添加/删除 HTML 元素
Add /Remove dynamically HTML element with jQuery plz see below
Add /Remove dynamically HTML element with jQuery
您可以使用三个变量作为当前 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.
它工作正常(请优化此代码):)
Its working fine (Plz optimized this code) :)