jquery动态生成li

根据后台数据动态生成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);  
        }
    }
});

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值