Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery获取表单中第一个唯一的多值(以数组表示)输入?_Javascript_Jquery_Forms_Input_Jquery Selectors - Fatal编程技术网

Javascript 如何使用jQuery获取表单中第一个唯一的多值(以数组表示)输入?

Javascript 如何使用jQuery获取表单中第一个唯一的多值(以数组表示)输入?,javascript,jquery,forms,input,jquery-selectors,Javascript,Jquery,Forms,Input,Jquery Selectors,我有一张这样的表格: <form> <select name="user_id[]"></select> <select name="user_id[]"></select> <select name="user_id[]"></select> <select name="user_id[]"></select> <select name="t

我有一张这样的表格:

<form>
    <select name="user_id[]"></select>
    <select name="user_id[]"></select>
    <select name="user_id[]"></select>
    <select name="user_id[]"></select>
    <select name="tag_id[]"></select>
    <select name="tag_id[]"></select>
    <select name="stack_id[]"></select>
    <!-- ... etc. -->
</form>

我想得到一个jQuery集合,它由按名称排列的第一个多值元素组成:

<form>
    <select name="user_id[]"></select>   <!-- this -->
    <select name="user_id[]"></select>
    <select name="user_id[]"></select>
    <select name="user_id[]"></select>
    <select name="tag_id[]"></select>    <!-- this -->
    <select name="tag_id[]"></select>    
    <select name="stack_id[]"></select>  <!-- this -->
    <!-- ... etc. -->
</form>


我是从
$('[name$=“[]”“]]”)开始的
,但我被困在那里了。是否有一个简单的选择器方法,或者我必须全部选择它们并过滤我想要的?

没有神奇的方法以这种方式选择元素,因此需要一些手动工作

首先,按名称对元素进行分组:

var buckets = {}; // keys: "name" attribute values, values: arrays of elements
$('[name$="[]"]').each(function() {
    var name = this.name,
        bucket = buckets[name] || (buckets[name] = []);
    bucket.push(this);
});
然后可以从每个bucket中选择第一个元素,并将其放入jQuery对象中:

// start with an empty jQuery object
var $firstOfEach = $([]);

// add to the above the first element inside each bucket
$.each(buckets, function() { $firstOfEach = $firstOfEach.add(this[0]); });

其次。。。输入元素的正确术语是什么:
id[]
?当有人告诉我的时候,我会编辑我的问题以使其更清楚。我不确定是否有一个特定的术语用于以这种方式命名的元素,但这并不重要。问题的核心适用于具有相同名称的所有元素,无论其是否以方括号结尾。带有输入元素的POST表单仍然会发布两个foo值bar和baz,只是服务器只会选择其中一个。
[]
符号只是让服务器端引擎知道需要多个,所以请使用不同的数据类型。谢谢。这真的只是为了一点无关紧要的教育,所以我可以用它作为谷歌搜索的参考。