• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
初心Syn
博客园    首页    新随笔    联系   管理    订阅  订阅
组装数据和页面渲染

组装数据

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);    });

不忘初心,方得始终,初心易得,始终难守。
posted on 2017-12-07 17:05  初心Syn  阅读(520)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3