使用JQuery我正在克隆这个字段集:
<fieldset class="pollQuestion" id="pq1">
<ul>
<label>Answers</label>
<li id="answerFields">
<input type="text" id="formanswer1" value="" />
</li>
<li>
<input type="button" class="addAnswer" value="Add Answer" />
<input type="button" class="delAnswer" value="Remove Answer" />
</li>
</ul>
</fieldset>
当我克隆()这个字段集时,如何在li#answerFields中添加输入字段?
我希望我的克隆看起来像这样:
<fieldset class="pollQuestion" id="pq1">
<ul>
<label>Answers</label>
<li id="answerFields">
<input type="text" id="formanswer1" value="" />
<input type="text" id="formanswer2" value="" />
<input type="text" id="formanswer3" value="" />
</li>
<li>
<input type="button" class="addAnswer" value="Add Answer" />
<input type="button" class="delAnswer" value="Remove Answer" />
</li>
</ul>
</fieldset>
任何和所有帮助都很有用!
答案 0 :(得分:2)
如果您的克隆位于$ your_clone:
var $answers = $your_clone.find("#answerFields");
var n_answers = $answers.find("input").length; // Here you get max number
var number_of_new_answers = XXX; // Your number of answers
for (i = n_answers + 1; i <= n_answers + number_of_new_answers; i++) {
var $new_answer = $('<input type="text" value="" id="formanswer' + i + '" />', {
type:"text",
id:"formanswer" + i,
value:""
});
$new_answer.appendTo($answers);
}