【问题标题】:return all values of input fields within div返回 div 中输入字段的所有值
【发布时间】:2011-02-23 10:32:41
【问题描述】:

我需要用 div 层中的所有值输入字段形成一个字符串 - 使用 jquery

<div id="selection">
   <input class="field" id="1" type="hidden" value="A"/>
   <input class="field" id="2" type="hidden" value="B"/>
   <input class="field" id="3" type="hidden" value="C"/>
   <input class="field" id="4" type="hidden" value="D"/>
</div>

<input type="button" id="button" value="generate"/>


这种形式:

id[1]=val[A]&id[2]=val[b]...so on


jquery:

 $(function() {
      $('#button').click(function() {
         //function goes here...


      });
    });

【问题讨论】:

  • 使用 jQuery 中的 class 属性取回值

标签: javascript jquery input input-field


【解决方案1】:

如果您使用name 代替(或除此之外)id

<input class="field" name="1" type="hidden" value="A"/>
<input class="field" name="2" type="hidden" value="B"/>
<input class="field" name="3" type="hidden" value="C"/>
<input class="field" name="4" type="hidden" value="D"/>

你可以使用serialize:

$('#button').click(function() {
   alert($('#selection input').serialize());
});

给你

1=A&2=B&3=C&4=D

如果你真的想要id[x]结构,你可以给元素命名id[1]id[2]等等。

编辑:哦,不知何故我忽略了你也想要val[x]。这对于serialize 是不可能的,只有当您真正将val[x] 作为字段中的值时。但是你为什么需要这样一个混淆的结构呢?


顺便说一句。您的按钮缺少type="button"

【讨论】:

  • 非常感谢,这就够了
【解决方案2】:
<script>
    $(function() {
        $('#button').click(function() {

            var str = new Array();
            var count = 0;

            $('.field').each(
                function()
                {                   
                    str[count] = 'id['+$(this).attr('id')+']=val['+$(this).val()+']';
                    count++;
                }
            );
            alert(str.join('&'))
        });
    });
</script>
<div id="selection">
   <input class="field" id="1" type="hidden" value="A"/>
   <input class="field" id="2" type="hidden" value="B"/>
   <input class="field" id="3" type="hidden" value="C"/>
   <input class="field" id="4" type="hidden" value="D"/>
</div>
<input id="button" value="generate" type="button"/>

【讨论】:

    【解决方案3】:

    另一种提供精确指定输出并优雅处理缺失属性的解决方案:

    See it in action at jsFiddle:

    $(function() {
        $('#button').click(function() {
    
            var ResStr  = $('#selection input.field');
    
            ResStr  = ResStr.map (function () {
                var jThis   = $(this);
                var ID      = jThis.attr ("id");
                if (!ID)    ID  = "null";
    
                var VAL     = jThis.val ()
                if (!VAL)   VAL  = "null";
    
                return 'id[' + ID + ']=val[' + VAL + ']';
    
            } ).get () .join ('&');
    
            alert (ResStr);
    
        } );
    } );
    

    【讨论】:

      【解决方案4】:

      这将返回 div 内所有输入的所有 html 组合

      var h = '';
      var c = 0;
      $('#selection input.field').each(function(){
       h += '&id['+(++c)+']=val['+$(this).val()+']';
      });
      h = h.slice(1);
      
      alert(h);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 1970-01-01
        • 2016-11-10
        • 2017-11-27
        • 2021-04-14
        相关资源
        最近更新 更多