如何限制 JQuery 克隆 Div 的数量?
我是 JQuery 新手,在使用 Clone() 函数时遇到一些问题。
我的问题是如何限制可以克隆答案的次数? 我会声明一个变量并通过循环运行它直到达到所需的数字吗?
这是 .aspx
> <div id="answer_wrapper">
> <div id="answer">
> <h3 class="new">Answer 1</h3>
> <asp:TextBox ID="QuestionAnswer1" runat="server" CssClass="form" Width="300px"></asp:TextBox>
> </div>
> <h3 class="new">Answer 2</h3>
> <asp:TextBox ID="QuestionAnswer2" runat="server" CssClass="form" Width="300px"></asp:TextBox>
> </div> <!-- end answer_wrapper -->
> <br />
> <asp:Button ID="ga" runat="server" CssClass="button_add_question" style="border: 0px;" />
> <a id="foo" href="#">Duplicate</a>
> <script type="text/javascript">
> $('#foo').click(function () {
> $('#answer').clone().appendTo('#answer_wrapper');
> });
> </script>
任何帮助将不胜感激,因为我真的不知道如何解决这个问题。
I'm new to JQuery and I'm having some trouble with the Clone() function.
My question is how can I limit the number of times the answers can be cloned?
Would I declare a variable and run it through a loop until the desired number is reached?
Here is the .aspx
> <div id="answer_wrapper">
> <div id="answer">
> <h3 class="new">Answer 1</h3>
> <asp:TextBox ID="QuestionAnswer1" runat="server" CssClass="form" Width="300px"></asp:TextBox>
> </div>
> <h3 class="new">Answer 2</h3>
> <asp:TextBox ID="QuestionAnswer2" runat="server" CssClass="form" Width="300px"></asp:TextBox>
> </div> <!-- end answer_wrapper -->
> <br />
> <asp:Button ID="ga" runat="server" CssClass="button_add_question" style="border: 0px;" />
> <a id="foo" href="#">Duplicate</a>
> <script type="text/javascript">
> $('#foo').click(function () {
> $('#answer').clone().appendTo('#answer_wrapper');
> });
> </script>
Any help would be much appreciated as I don't really know how to approach this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的代码将导致创建具有相同 ID 的多个元素,这并不理想,但要回答您的问题,也许可以这样做:
It looks like your code is going to cause multiple elements with the same ID to be created, which isn't ideal, but to answer your question, maybe do this:
您可以通过在点击事件外部设置 var 并增加其值来限制克隆的 div 数量,也可以计算 div 数量。
我希望您也想更改或删除该 ID。
计算div:
You can either limit the number of divs cloned by setting a var outside click event and increasing its value or you can count the divs number.
I expect you want to change or remove the id too.
counting divs: