js 遍历数组对象
有一个JSON数组如下 all = {"error":0,"content":[{"name":"北京","value":"1036","is_checked":"checked"},{"name":"上海","value":"1035","is_checked":""},{"name":"天津","value":"1034","is_checked":""},{"name":"重庆","value":"1033","is_checked":""},{"name":"河北","value":"1032","is_checked":""},{"name":"河南","value":"1031","is_checked":""},{"name":"广东","value":"1030","is_checked":""},{"name":"广西","value":"1029","is_checked":""}]} contents = all.content; jQuery方法: jQuery.each(contents,function(i,n){ //其中i为键 n为值 write_message += "<input type=‘checkbox‘ name=‘id[]‘ value=‘"+contents[i].value+"‘ "+contents[i].is_checked+" />"+contents[i].name+" "; }); JS方法 for (var i in contents) { write_message += "<input type=‘checkbox‘ name=‘id[]‘ value=‘"+contents[i].value+"‘>"; } for (var i=0; i<contents.length; i++) { write_message += "<input type=‘checkbox‘ name=‘id‘ value=‘"+contents[i].value+"‘ "+contents[i].is_checked+" />"+contents[i].name+" "; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。