我有下面的代碼附加問題/答案的股利。jquery appendTo html定購問題
它的工作原理,但它寫入的股利錯誤的順序。它把任何新的div最後一過,所以編號變得無序這樣的:
Question 3
Question 2
Question 1
我打得四處appendTo,追加,但任何事情我做不工作。
任何幫助,將不勝感激。謝謝!
下面的代碼:
$(".questionButton").click(function() {
var parentId = $("button").closest("div").attr("id");
var newTextBoxDiv = $("<div>").attr("id", 'question' + questionCounter);
$(this).after(newTextBoxDiv);
newTextBoxDiv.after().html(
"<table><tr>" +
"<td>" +
"<label>Question " + questionCounter + ": </label>" +
"</td><td>" +
"<input type='text' name='tbQuestion' id='tbQuestion" + questionCounter + "' value='' >" +
"</td>" +
"<td class='removeQuestion'>x</td>" +
"</tr><tr><td>" +
"<label>Answer: </label>" +
"</td><td>" +
"<input type='text' name='tbAnswer'" id='tbAnswer" + questionCounter + "' value='' >" +
"</td></tr></table>");
newTextBoxDiv.appendTo("#" + parentId);
questionCounter++;
});
HTML:
<div id="questionBox">
<div id="mcq" style="display: none;" class="questionTypeDiv">
<input type="button" class="questionButton" value="Add Question" style="margin-top: 4px;" />
<table></table>
</div>
</div>
[prepend()](http://api.jquery.com/prepend/)是否將它們按正確的順序排列? – Leng
你能提供['jsfiddle'](http://jsfiddle.net)的例子嗎? – Dom
'$(this)'是指什麼?您需要發佈所有代碼,但根據您提供的內容,我們無法幫助您。 – Leng