组装数据
var contacts = new Array();
$("#contact").find('div').each(function() {
if ($(this).hasClass('contList')) {
if ($(this).find('div').find('input[name="linkman"]').val() || $(this).find('div').find('input[name="phone"]').val() || $(this).find('div').find('input[name="email"]').val()) {
var contactsJsonStr = dataHelper.setJson(null, "linkman", $(this).find('div').find('input[name="linkman"]').val());
contactsJsonStr = dataHelper.setJson(contactsJsonStr, 'phone', $(this).find('div').find('input[name="phone"]').val());
contactsJsonStr = dataHelper.setJson(contactsJsonStr, 'email', $(this).find('div').find('input[name="email"]').val());
contacts.push(jQuery.parseJSON(contactsJsonStr));
}
}
});
jsonstr = dataHelper.setJson(jsonstr, "contacts", contacts); //联系方式集合
页面渲染数据
$.each(data.payload.contacts, function(index, entry) {
$("#contact").find('div').each(function() {
if ($(this).hasClass('contList')) {
if ($(this).index() == index) {
$(this).find('div').find('input[name="linkman"]').val(entry.linkman);
$(this).find('div').find('input[name="phone"]').val(entry.phone);
$(this).find('div').find('input[name="email"]').val(entry.email);
}
}
})
});
知识点:
function (index, value)index前元素位置value值
// each处理维数组 var arr1 = [ "aaa", "bbb", "ccc" ];
$.each(arr1, function(i,val){ alert(i); alert(val); });
// 处理json数据例ajax返值 var obj = { one:1, two:2, three:3};
$.each(obj, function(key, val) { alert(key); alert(val); });