首页 > 通过json循环搜寻元素问题

通过json循环搜寻元素问题

// 顾客
<input name="customer[customerId]">
<input name="customer[customerName]">

// 顾客地址
<input name="customer[address][addressId]">
<input name="customer[address][address1]">
<input name="customer[address][address2]">
<input name="customer[address][address3]">

// 所在地区
<select name="customer[district][districtId]">
  <option value="">选择地区</option>
  <option value="1">地区1</option>
  ....
</select>

提交后, Ajax返回json, 以下为没有填写的内容

{messages: {
 'customer' : {
     // 第2层
     'customerName' : { 'isEmpty' : '不能为空'} ,
     'address' : {
        // 第3层
        'address3' : { 'isEmpty' : '不能为空'} 
     },
     'district' : {
         // 第3层
        'districtId' : { 'isEmpty' : '不能为空'} 
     },
 }
}}
// 如何透过jQuery循环搜寻对应的元素,然后在元素后加上错误提示
$(':input[name="customer[customerName]"]')
.eq(0)
.append('<span class="error">不能为空</span');

$(':input[name="customer[address][address3]"]')
.eq(0)
.append('<span class="error">不能为空</span');

$(':input[name="customer[district][districtId]"]')
.eq(0)
.append('<span class="error">不能为空</span');

http://jsfiddle.net/EeYs8/5/ 当去到第3层,我不知道有什么方法判断并返会第2层,找不到第3个customer[district][districtId](我的代码本身有问题)


建议用underscore库先把数据选出来,filter,where等函数非常好用。

【热门文章】
【热门文章】