简体   繁体   English

每次单击按钮时添加新 div

[英]Adding new div every time button is clicked

I am trying to create a page where the user can create a question form where they add questions by themselves and change the type of questions they want to ask.我正在尝试创建一个页面,用户可以在其中创建一个问题表单,他们可以在其中自行添加问题并更改他们想要提出的问题类型。 Pretty much the same as Google Forms.与 Google 表单几乎相同。 Based on the type of question, a box should appear beneath the question that is created.根据问题的类型,创建的问题下方应出现一个框。 When I try to do this, the box only appears on the last question, so i believe it is overwritten every time a new question is added.当我尝试这样做时,该框仅出现在最后一个问题上,因此我相信每次添加新问题时它都会被覆盖。 Any tips on how to create box beneath every question?关于如何在每个问题下方创建框的任何提示?

 function createQuest() { number++; chooseTime(); outputQuestion += '<div id="question' + number + '>'; outputQuestion += '<p id="k"></p>'; outputQuestion += '<h2>Question' + number + '</h2>'; outputQuestion += '<form name="choose">'; outputQuestion += '<textarea rows="1" cols="30" id="question' + number + '" name="text" placeholder="Question"></textarea>'; outputQuestion += '<select id="sel' + number + '">'; outputQuestion += '<option id="' + number + '" id="TEXT" onclick()>Text</option>'; outputQuestion += '<option id="' + number + '" id="SLIDER">Rating</option>'; outputQuestion += '</select>'; outputQuestion += '<ul id="list-quest' + number + '">'; outputQuestion += '</ul>'; outputQuestion += '</form>'; outputQuestion += '</div>'; $("#list-create-doc").html(outputQuestion); textChosen(number); } var outputQuest1 = ""; function textChosen(nr) { outputQuest1 = ""; outputQuest1 += '<div class="textbox" id="txt">'; outputQuest1 += '<textarea rows="1" cols="30" placeholder="Answered with text"></textarea>'; outputQuest1 += '</div>'; var whereAdd = "#list-quest" + nr; console.log(whereAdd); $(whereAdd).html(outputQuest1); }
 <button id="btn" onclick="createQuest()">Legg til spørsmål</button>

You are overriding the current content by using html() .您正在使用html()覆盖当前内容。 Use append() to add a new one like this:使用append()添加一个新的,如下所示:

$("#list-create-doc").append(outputQuestion);

Here is the docs for jQuery.append and jQuery.html !这是jQuery.appendjQuery.html的文档!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM