qq_34963264
coding_last
采纳率50%
2020-02-07 16:34

js如何解析后端逗号分隔数据?

JSON数据:想要把inputList中数据赋值到select下拉框中

// 20200207152810
// http://localhost:9090/productAttr/list/2?type=1

{
  "code": 200,
  "message": "操作成功",
  "data": [
    {
      "id": 5,
      "productAttributeCategoryId": null,
      "inputList": "背带,哈伦裤,工装裤,阔腿裤,铅笔裤,运动",
      "name": "款式",
      "type": 1
    },
    {
      "id": 6,
      "productAttributeCategoryId": null,
      "inputList": "短裤,七分裤,长裤",
      "name": "裤长",
      "type": 1
    },
    {
      "id": 7,
      "productAttributeCategoryId": null,
      "inputList": "2020上市",
      "name": "时间",
      "type": 1
    },
    {
      "id": 8,
      "productAttributeCategoryId": null,
      "inputList": "时尚潮流",
      "name": "风格",
      "type": 1
    }
  ]
}

前端代码用jquery遍历的

function putParam(result) {


    $.each(result.data, function (index, obj) {
        var test = ' <div class="layui-form-item">     <label class="layui-form-label">' + obj["name"] + '</label>' +
            '        <div class="layui-input-block" >' +
            '            <select name="city" lay-verify="required" id="param-select">' +
            '                <option value="">请选择</option>' +
            '                <option value="">'+obj["inputList"]+'</option>' +
            '            </select>' +
            '        </div></div>';

        $('#param-body').append(test);
    })
}

现在的问题是下拉框中的obj["inputList"]是所有的数据,如何拆分并根据name属性分别赋值?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • jslang 天际的海浪 1年前
            var test = ' <div class="layui-form-item">     <label class="layui-form-label">' + obj["name"] + '</label>' +
                '        <div class="layui-input-block" >' +
                '            <select name="city" lay-verify="required" id="param-select">' +
                '                <option value="">请选择</option>' +
                obj["inputList"].split(",").reduce(function(pv, v, i){ return pv + '<option value="">'+v+'</option>';},"") +
                '            </select>' +
                '        </div></div>';
    
    点赞 1 评论 复制链接分享

相关推荐