jQuery put dynamically created list inside <ul> tags
.append()
does not seem to do the trick, it place both <ul>
&</ul>
at the the top.
how should I do it?
(The fullQuestion
is only one <li>
it is generated for avery question)
function writeQuestions() {
$('#container').append("<ul>\n");
len = questions.qestion.length;
for (var i = 0; i < len; i++) {
var answerRdy = [];
var qestionRdy = questions.qestion[i];
answerRdy[0] = questions.answer[i][0];
answerRdy[1] = questions.answer[i][1];
answerRdy[2] = questions.answer[i][2];
answerRdy[3] = questions.answer[i][3];
var divID = "question-" + i;
var formID = "form-" + i;
var CurrentForm = i;
var writeAnswer = [];
writeAnswer[writeAnswer.length] = ("\n<br />`<开发者_如何学运维;li id='{0}'>`\n<form id='{1}' name='{2}'>\n").format(divID, formID, CurrentForm);
writeAnswer[writeAnswer.length] = ("<b>" + qestionRdy + "</b><br />\n");
for (var n=0; n<=3; n++) {
writeAnswer[writeAnswer.length] = ("<input type='radio' name='answerN' value='{0}' /> {1} <br />\n").format(n, answerRdy[n]);
}
writeAnswer[writeAnswer.length] = ("<input type='submit' value='Submit your answer'>\n</form>\n</li><!--{0}-->").format(divID);
var joinQuestion = writeAnswer.join();
// var joinQuestion = "<ul>\n" + joinQuestion + "\n</ul>"
exp = /,/gi;
var fullQuestion = joinQuestion.replace(exp, "");
$('#container').append(fullQuestion);
}
for (var i=0; i < len; i++) {
var formID = document.forms["form-" + i];
$(formID).bind("submit", validate);
}
$('#container').append("\n</ul>");
}
You don't use append properly.
Create a UL first
var $ul = $('<ul>');
$('#container').append($ul);
//all other code
$ul.append(fullQuestion);
And remove this line $('#container').append("\n</ul>");
Try this it should insert the fullQuestion string inside the ul
$('#container ul').html(fullQuestion);
精彩评论