Note that there are some explanatory texts on larger screens.

plurals
  1. POTo add&remove nested(two-level) input elements dynamically?
    primarykey
    data
    text
    <p>I searched the question several times, but didn't get the solution, that's why I'm asking here. I already know how to add&amp;remove one-level input elements, as follow:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('#btn_addOpt').click(function () { var opt = $('&lt;div&gt;&lt;/div&gt;'); opt.append('&lt;p&gt;Answer:&lt;/p&gt;'); opt.append('&lt;input type="text" name="lable[]"&gt;'); $('#opt').append(opt); }); $('#btn_removeOpt').click(function () { $('#opt div:last').remove(); }); }); &lt;/script&gt; </code></pre> <p>And the body of html is:</p> <pre><code>&lt;form action="" method="post"&gt; What's the question?&lt;br/&gt; &lt;input type="text" name="name"/&gt;&lt;br /&gt; &lt;input type="button" id="btn_addOpt" value="Add Answer"/&gt; &lt;input type="button" id="btn_removeOpt" value="Remove Answer"/&gt;&lt;br/&gt; &lt;div id="opt"&gt;&lt;/div&gt;&lt;br/&gt; &lt;input type="submit" value="submit"/&gt; &lt;/form&gt; </code></pre> <p>The idea is when I click the 'Add Answer' button, a group of elements:</p> <pre><code>&lt;p&gt;Answer:&lt;/p&gt; &lt;input type="text" name="lable[]"&gt; </code></pre> <p>will be added, and also will be removed after I click the 'Remove Answer' button. However, what I try to do is to take all elements above as a group, which inclues the:</p> <pre><code>What's the question?&lt;br/&gt; &lt;input type="text" name="name"/&gt;&lt;br /&gt; &lt;input type="button" id="btn_addOpt" value="Add Answer"/&gt; &lt;input type="button" id="btn_removeOpt" value="Remove Answer"/&gt;&lt;br/&gt; &lt;div id="opt"&gt;&lt;/div&gt;&lt;br/&gt; </code></pre> <p>And I try to add&amp;remove this group dynamically. So I try:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('#btn_addQues').click(function () { var que = $('&lt;div&gt;&lt;/div&gt;'); que.append('&lt;p&gt;What\'s the question?&lt;/p&gt;'); que.append('&lt;input type="text" name="name"/&gt;'); que.append('&lt;input type="button" id="btn_addOpt" value="Add Answer"/&gt;'); que.append('&lt;input type="button" id="btn_removeOpt" value="Remove Answer"/&gt;&lt;br/&gt;'); que.append('&lt;div id="opt"&gt;&lt;/div&gt;&lt;br/&gt;'); $('#question').append(que); }); $('#btn_removeQues').click(function () { $('#question div:last').remove(); }) $('#btn_addOpt').click(function () { var opt = $('&lt;div&gt;&lt;/div&gt;'); opt.append('&lt;p&gt;Answer:&lt;/p&gt;'); opt.append('&lt;input type="text" name="lable[]"&gt;'); $('#opt').append(opt); }); $('#btn_removeOpt').click(function () { $('#opt div:last').remove(); }); }); &lt;/script&gt; </code></pre> <p>And the body become:</p> <pre><code>&lt;body&gt; &lt;input type="button" id="btn_addQues" value="Add Question"/&gt; &lt;input type="button" id="btn_removeQues" value="Remove Question"/&gt; &lt;form action="" method="post"&gt; &lt;div id="question"&gt;&lt;/div&gt; &lt;input type="submit" value="submit"/&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>However, it can add&amp;remove the whole group of elements dynamically, but inside the group, the "Add Answer" and "remove answer" buttons don't work. Seems there are some conflicts. Or there might be some other ways that can do it. Basically, What I try to do is to add&amp;remove a group of elements by buttons, and inside the group there are also two buttons to add&amp;remove some other input elements. So it's a nested(two-level) elements creation. And all information filled in will be posted to another page. I'm not sure if I explain clearly, but anyone could help? Please provide some codes, thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload