ul中的li动态获取后台数据$.each()的使用方式

本文介绍了一个使用Ajax实现的实时获取并展示车辆信息的JavaScript函数。该函数通过POST请求从后台获取JSON格式的车辆数据,并将其格式化后显示在一个指定的HTML元素中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    my.leftCarRealTimeInformation = function(){
        $.ajax({
            url: '----------后台给的数据地址------------',     
            type: 'post',                 //获取数据方式:post/get          
            async: false,                  //加载方式默认异步,true为同步
            dataType:'json',            //数据格式
            success:function(data){   
                var carNewsList = "";
                $.each(data,function(i,item){   //遍历ul中的li
                    carNewsList += "<li  style='font-size:14px;color:#5d5d5d;'>"+item.columnName+"</li>";
                });
               $('#leftCarInformationList').html(carNewsList);        //数据在<ul id="leftCarInformationList"></ul>里面显示
            },
            error: function(obj){
                alert("return error:"+obj);
            },    
        });
    };
var editor; var action = $.queryString("action"); var Pid = $.queryString("id"); $(function () { //设置返回列表的href, location_prior的href在后台打开当前记录信息时赋值。 // $("#location_back").attr("href", $("#location_prior").attr("href")); //用户获取出来,获取本部门用户。 //获取本部门的权限, $.ajax({ type: "POST", url: "../services/Dtry.ashx", headers: { "Authorization": "0772A4AA051DDEBB393DC164B19A32E57C4FEEAB27A77820" }, data: { action: "names", id: Pid }, success: function (msg) { $.each(msg.ds, function (index, item) { if (item.pid == -1) { $('#qx').append('<div><ul><li> <input id="a' + item.id + '" type="checkbox" name="select_checkbox" onclick="chk_changed();" value="' + item.id + '" /><a class="sz" href="javascript:onClick=show(' + item.id + ')">' + item.name + '</a></li></ul><ul id="' + item.id + '"> </ul></div>'); } else { $('#' + item.pid).append('<li class="sc"> <input id="a' + item.id + '" type="checkbox" name="select_checkbox" onclick="chk_changed(this);" value="' + item.id + '"/><a class="zt" href="javascript:onClick=show(' + item.id + ')">' + item.name + '<br/><ul class="cc" style="display:none" id="' + item.id + '"> </ul></li>') } }) $('#typeid').val(Pid); } }); $('#user_nickname').on('change', getQ); //初始化表单验证 $("#form1").initValidform(); $(".rule-single-checkbox").ruleSingleCheckbox(); $(".rule-single-select").ruleSingleSelect(); // var result = ""; }); function chk_changed(that) { var th = $(that); if (th.is(':checked')) { console.log('已选中'); } else { console.log('未选中'); } th.next('a').find('input').attr("checked", "true"); th.parent().parent().siblings().find('input').attr("checked", "true"); th.parent().parent().parent().siblings().attr("checked", "true"); var result = ""; $("input[name='select_checkbox']:checked").each(function () { result += $(this).val() + ','; }); if (result != "") { result = result.substring(0, result.lastIndexOf(',')); } $("#typename").val(result); } function show(d1) { if (document.getElementById(d1).style.display == 'none') { document.getElementById(d1).style.display = 'block'; //触动的层如果处于隐藏状态,即显示 } else { document.getElementById(d1).style.display = 'none'; //触动的层如果处于显示状态,即隐藏 } } var getQ = function () { var userid = $('#user_nickname').val(); if (userid == null || userid == undefined) { return false; } else { $.ajax({ type: "POST", url: "../services/Dtry.ashx", headers: { "Authorization": "0772A4AA051DDEBB393DC164B19A32E57C4FEEAB27A77820" }, data: { action: "getQ", id: userid }, success: function (msg) { $.each(msg.ds, function (index, item) { $('#a' + item.typeid).attr("checked", true); }) var result = ""; $("input[name='select_checkbox']:checked").each(function () { result += $(this).val() + ','; }); if (result != "") { result = result.substring(0, result.lastIndexOf(',')); } $("#typename").val(result); } }); } } 详细翻译代码内容
03-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值