【问题标题】:Jquery SPLIT field namesJquery SPLIT 字段名称
【发布时间】:2015-08-11 23:17:40
【问题描述】:

您好,我想分解输入:名称以 [] 作为分隔符的数组

示例 名称 recipe[0][recipe_photo] 我想转换为对象 ['recipe',0,'recipe_photo']

$('input', form).each(function () {
  var arr = $(this).data("name").split('');
  console.log(arr);
});

<form>
<input name='recipe'/>
<input name='recipe[]'/>
<input name='recipe[0][recipe_photo]'/>
<input name='recipe[1][recipe_photo]'/>
</form>

【问题讨论】:

  • $('input').each(function () { var arr = $(this).prop("name"); console.log(arr); });这就是你想要的
  • 我不需要这个。我想将字段的名称分解为 Array。

标签: javascript jquery split


【解决方案1】:

这是我的解决方案

var parts = arr.split(/[[\]]{1,2}/);
console.log(parts); // ["recipe","0","recipe_photo",""] 

【讨论】:

    【解决方案2】:

    你可以这样做:

        function go(str)
        {
          if (!str) return;
          var arr= str.split(/[\[\]]+/g).filter(function(v){return v!==''});
          return 'object['+arr.join(',')+']';
        }
    
    console.log(go('recipe[]')); //object[recipe]
    console.log(go('recipe[0][recipe_photo]')); //object[recipe,0,recipe_photo]
    console.log(go('recipe[1][recipe_photo]')); //object[recipe,1,recipe_photo]
    

    jsbin

    【讨论】:

      【解决方案3】:

      对不起,如果我误解了您的问题,但我已尽力为您提供解决方案。

      Here 是。

      var arrayWithNames = [];
      $('input').each(function () { arrayWithNames.push($(this).prop("name"));});
      

      如您所见,我将每个 'name' 属性推送到数组 'arrayWithNames'。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        相关资源
        最近更新 更多