您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery怎么遍历出从后台取得list集合?

2024/3/25 6:59:57发布12次查看
项目中jquery从后台取得list,是怎么遍历出来的?
一般情况下,后台的list应该是转成了json字符串,返回到了ajax的回调函数里。在回调函数里可以直接操作该json字符串。
举个例子:
$.post("test.php", { name: "john", time: "2pm" },
function(data){ //可以在这儿循环,比如: var listnow=data.listhoutai;//取list。listhoutai是你后台定义的json名称 for ( var i = 0; i < listnow.length; i++) { var id = vos[i].id;//可以取list中第一个对象的id值,其他的类推 } });
可以来一个jquery自定义数组操作类js外部文件,前提需要引入jquery类库。封装类代码如下:
(function ($) { $.list = function () { var _list = new array(); //外部为数组赋值 this.getdatasource = function (arr) { if (isarraytype(arr)) { _list = arr; } else { alert("指定元素非数组类型,赋值失败!"); } }; //添加一个元素 this.add = function (arg) { if (arg) { _list.push(arg); } else { alert("参数错误,添加元素失败!"); } return _list; }; //删除指定索引的元素 this.removeat = function (index) { if (isarrayindex(index) && index < _list.length) { var i; var arr = new array(); for (i = 0; i < _list.length; i++) { if (i != index) { arr.push(_list[i]); } } _list = arr; return _list; } else { alert("未获取到设置对象的实例,删除元素失败!"); } }; //按照指定的分割符显示出所有元素 this.split = function (arg) { arg = arg || ","; var i, res; res = ""; if (_list.length > 0) { for (i = 0; i < _list.length; i++) { res += _list[i].tostring() + arg; } return res.substr(0, (res.length - arg.tostring().length)); } else { return ""; } }; //外部调用直接返回当前数组实力 this.toarray = function () { return _list; }; //设置指定索引处的值为指定值 this.update = function (index, value) { if (isarrayindex(index) && index < _list.length) { _list[index] = value; } return _list; }; //清空所有元素 this.removeall = function () { _list.splice(0, _list.length); return _list; }; //根据传入的值获取第一次出现在数组中的下标 this.indexof = function (value) { if (value) { var i; for (i = 0; i < _list.length; i++) { if (_list[i] == value) { return i; } } } return -1; }; //获取数组长度 this.size = function () { return _list.length; }; //移除数组中重复的项 this.removerepeat = function () { _list.sort(); var rs = []; var cr = false; for (var i = 0; i < _list.length; i++) { if (!cr) cr = _list[i]; else if (cr == _list[i]) rs[rs.length] = i; else cr = _list[i]; } for (var i = rs.length - 1; i >= 0; i--) this.removeat(rs[i]); return _list; }; //对数字数组元素排序,参数:0升序1降序 this.sortnumber = function (f) { if (!f) f = 0; if (f == 1) return _list.sort(function (a, b) { return b - a; }); return _list.sort(function (a, b) { return a - b; }); }; //私有方法 //判断正确的数组下标 function isarrayindex(index) { var reg = /^\d+$/; if (reg.test(index)) return true; else return false; } //判断当前对象是否为数组对象 function isarraytype(arr) { if (typeof arr == 'object' && typeof arr.length == 'number') return true; else return false; } }; //结束list的构造方法 })(jquery);
页面调用时需要引入两个js文件:
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script> <script src="js/jquery.array.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var mylist = new $.list(); mylist.add(1); mylist.add("1906-07-08"); mylist.add("hellow world"); mylist.removeat(0); mylist.update(0, "11111111"); //alert("数组被修改内容后的结果:" + mylist.split("|")); mylist.removeall(); var arr = mylist.toarray(); //alert("数组全部被删除后结果:" + arr); //alert("数组1当前长度:" + mylist.size()); var mylist2 = new $.list(); mylist2.add(3); mylist2.add(1); mylist2.add(45); mylist2.add(21); mylist2.add(-9); mylist2.add(1); alert("第二个实例数组结果:" + mylist2.toarray()); mylist2.removerepeat(); alert("去重后第二个实例数组结果:" + mylist2.toarray()); alert("去重后第二个实例数组长度:" + mylist2.size()); mylist2.sortnumber(1); alert("排序后的数组:" + mylist2.toarray()); var arr3 = ["aaa", "bbb", "ccc", "ddd", "eee"]; var arr4; mylist2.getdatasource(arr3); alert("重新赋值后结果:"+mylist2.toarray()); }); </script>
对象语法json数据格式(当服务器端回调回来的对象数据格式是json数据格式,必须保证json的格式要求,回调的对象必须使用eval函数进行转化(否则将得不到object)。本文不作详细介绍服务器端回调的数据问题,我们将直接自定义对象)
1.jquery遍历对象
< !doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html > <head > <title > new document < /title> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历对象 .each的使用------------- var obj =[{"name ":"项海军","password ":"123456 "}]; $("#result ").html("------------遍历对象.each的使用-------------"); alert(obj);//是个object元素 //下面使用each进行遍历 $.each(obj,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " + value.name +" < /td> <td>" + value.password +"</td > </tr>"; tbody += trs; }); $("#project").append(tbody); }); </script > </head> <body> <div id="result" style="font-size:16px;color:red;"></div > <table cellpadding = 5 cellspacing = 1 width = 620 id = "project"border = "1" > <tr > <th > 用户名 < /th> <th>密码</th > </tr> </table > </body> </html >
2.jquery遍历数组
< !doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html > <head > <title > new document < /title> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历数组 .each的使用------------- var anarray = ['one','two','three']; $("#result ").html("------------遍历数组.each的使用-------------"); $.each(anarray,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " +value+" < /td></tr > "; tbody += trs; }); $("#project ").append(tbody); }); </script> </head> <body> ------------此部分同1中的body部分-------------------- </body> </html> 3.jquery 遍历list集合(其实与遍历一个对象没有太大区别,只是格式上的问题) <!doctype html public " - //w3c//dtd html 4.0 transitional//en"> < html > <head > <title > new document < /title> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历list集合 .each的使用------------- var obj =[{"name ":"项海军","password ":"123456 "},{"name ":"科比","password ":"333333 "}]; $("#result ").html("遍历list集合.each的使用"); alert(obj);//是个object元素 //下面使用each进行遍历 $.each(obj,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " +value.name+" < /td> <td>" + value.password +"</td > </tr>"; tbody += trs; }); $("#project").append(tbody); }); </script > </head> <body> ------------此部分同1中的body部分-------------------- </body > </html>
以上就是jquery怎么遍历出从后台取得list集合?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product