jQuery 如何选择所有具有相同名称的输入并按名称索引它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9192731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:29:10  来源:igfitidea点击:

How to select all inputs having same name and index them by their name

jqueryjquery-selectors

提问by AlterPHP

I have to handle a form generated by a third part. This form provides some checkbox "multi-select" attributes. But input name's are identical, and PHP $_POST can't handle identical keys (only the last data is handled).

我必须处理由第三方生成的表单。此表单提供了一些复选框“多选”属性。但是输入名称是相同的,PHP $_POST 不能处理相同的键(只处理最后一个数据)。

Here is an example of the form :

这是表格的一个例子:

<form>
    <input type="checkbox" name="attr1" value="1" />
    <input type="checkbox" name="attr1" value="2" />
    <input type="checkbox" name="attr1" value="3" />
    <input type="checkbox" name="attr1" value="4" />
    <input type="checkbox" name="attr2" value="xx" />
    <input type="checkbox" name="attr3" value="a" />
    <input type="checkbox" name="attr3" value="b" />
    <input type="checkbox" name="attr3" value="c" />
    <input type="checkbox" name="attr3" value="d" />
    <input type="text" name="attr4" value="" />
</form>

I'd like to parse all checkbox inputs, get only group of same name inputs, and append "[]" to their name...

我想解析所有复选框输入,只获取一组相同名称的输入,并将“[]”附加到他们的名字...

What I want to get after jQuery processing :

我想在 jQuery 处理后得到什么:

<form>
    <input type="checkbox" name="attr1[]" value="1" />
    <input type="checkbox" name="attr1[]" value="2" />
    <input type="checkbox" name="attr1[]" value="3" />
    <input type="checkbox" name="attr1[]" value="4" />
    <input type="checkbox" name="attr2" value="xx" />
    <input type="checkbox" name="attr3[]" value="a" />
    <input type="checkbox" name="attr3[]" value="b" />
    <input type="checkbox" name="attr3[]" value="c" />
    <input type="checkbox" name="attr3[]" value="d" />
    <input type="text" name="attr4" value="" />
</form>

Is there a way to identify group of inputs with same name ?

有没有办法识别具有相同名称的输入组?

回答by Shawn Chin

This will append []to the name of each checkbox item if there exists another item with the same name.

[]如果存在另一个具有相同名称的项目,这将附加到每个复选框项目的名称。

var name_map = {};
$("input[type=checkbox]")  // for all checkboxes
    .each(function() {  // first pass, create name mapping
        var name = this.name;
        name_map[name] = (name_map[name]) ? name + "[]" : name;
    })
    .each(function() {  // replace name based on mapping
        this.name = name_map[this.name];
    });

Demo: http://jsfiddle.net/gnfsG/

演示:http: //jsfiddle.net/gnfsG/

回答by Christoph

Try this:

尝试这个:

var group = $('input[name="attr1"]');

if (group.length > 1){
   group.each(function () {
        $(this).attr("name",$(this).attr("name")+"[]");
   });
}

回答by Amar Palsapure

You will need to use $('input[name^="attr"]')in your case. As your items has attr1, attr2, attr3etc. The above selector will match all.

您将需要$('input[name^="attr"]')在您的情况下使用。当你的项目有attr1attr2attr3等。上述选择器将匹配所有。

You can check this in action @ http://jsfiddle.net/cT78Y/2/

您可以在@ http://jsfiddle.net/cT78Y/2/ 中查看此操作

$('input[name^="attr"]:checkbox').each(function(i){
   $(this).attr("name",$(this).attr("name")+"[]");
});

This will give you.

这会给你。

<form>
  <input type="checkbox" name="attr1[]" value="1">
  <input type="checkbox" name="attr1[]" value="2">
  <input type="checkbox" name="attr1[]" value="3">
  <input type="checkbox" name="attr1[]" value="4">
  <input type="checkbox" name="attr2[]" value="xx">
  <input type="checkbox" name="attr3[]" value="a">
  <input type="checkbox" name="attr3[]" value="b">
  <input type="checkbox" name="attr3[]" value="c">
  <input type="checkbox" name="attr3[]" value="d">
  <input type="text" name="attr4" value="">
</form>

Hope this helps you.

希望这对你有帮助。

回答by Poonam

This is very long solution

这是很长的解决方案

var array =[];
                $('input[type="checkbox"]').each(function(){
                    array.push($(this).attr('name'));
                });

                var sorted_arr = array.sort(); // You can define the comparing function here.
                // JS by default uses a crappy string compare.
                var results = [];
                for (var i = 0; i < array.length - 1; i++) {
                    if (sorted_arr[i + 1] == sorted_arr[i]) {
                        results.push(sorted_arr[i]);
                    }
                }
                var names = [];
                names = jQuery.unique(results);
                $.each(names ,function(key,value){
                    $('input[name ="'+value+'"]').attr("name",value+"[]");
                });

回答by runphp

$("input[name=attr\[\]]");

get all input where name = attr[]

获取名称 = 的所有输入 attr[]