根据后台数据动态生成li到ul,html:
<html>
<head></head>
<body>
<div class="template-text">
<ul id="ul_currentquestion">
</ul>
</div>
</body>
</html>
动态生成li到ul下:
$.ajax({
type: "POST",
url: "question/getbycondition",
data: JSON.stringify({
orderBy: 'questionorder asc',
condition: {
templateid: 'null',
isgeneralquestion: '0',
templateid: templateid
}
}),
async: false,
contentType: 'application/json',
dataType: "json",
error: function(data) {
alert(data.message);
},
success: function(response) {
if (response.code == '0000') {
//重新加载li
$("#ul_currentquestion li").remove();
var json = eval(response.data);
//alert("json="+json);
$.each(json,
function(index, item) {
//循环获取数据
var questiondesc = '问题' + (index + 1) + '. ' + json[index].questiondesc;
var questionid = json[index].questionid;
var generalquestionid = json[index].generalquestionid;
var li = $("<li value=" + json[index] + ">" + questiondesc + "</li>");
li.appendTo("#ul_currentquestion");
li.append(' <a onclick="deleteQuestion(\'' + questionid + '\')">删除</a>');
if (generalquestionid == null || generalquestionid == '') {
li.append(' <a onclick="updateQuestion(\'' + questionid + '\')">修改</a>');
}
var index = $("#ul_currentquestion li").index(li);
var nextId = null;
var previousId = null;
if (index < (json.length - 1)) {
nextId = json[index + 1].questionid
}
if (index > 0) {
previousId = json[index - 1].questionid;
}
if (json.length > 1) {
if (index == 0) {
li.append('<a onclick="upordown(\'' + questionid + '\',\'' + nextId + '\')">下移</a>');
} else if (index == json.length - 1) {
li.append('<a onclick="upordown(\'' + questionid + '\',\'' + previousId + '\')">上移</a>');
} else {
li.append('<a onclick="upordown(\'' + questionid + '\',\'' + previousId + '\')">上移</a>');
li.append('<a onclick="upordown(\'' + questionid + '\',\'' + nextId + '\')">下移</a>');
}
}
});
} else {
//alert("保存失败,"+data.message);
}
}
});