jquery - 从具有相同id的多个下拉列表中获取值

时间:2022-11-29 13:11:50

I have these dynamic dropdowns with dynamic ids and value.

我有动态ID和值这些动态下拉列表。

<select id=extra['123']></select>
<select id=extra['453']></select>
<select id=extra['789']></select>

In php, I am able to get the value using:

在PHP中,我可以使用以下方法获取值:

$_REQUEST['extra']

And I get an array.

我得到一个阵列。

[extra] => Array
    (
        [123] => 0
        [453] => 0
        [789] => 0
    )

But how do I create an array in jquery? Thanks in advance!

但是我如何在jquery中创建一个数组?提前致谢!

1 个解决方案

#1


4  

You can use wild card

你可以使用外卡

Live Demo

selectArray = $('[id^=extra]');

Iterating throuh all selects

通过所有选择迭代

selectArray.each(function() {
    alert(this.id);
})​

For getting the numbers in id of selects

用于获取选择ID的数字

Live Demo

selectArray = $('[id^=extra]');
ids = selectArray.map(function() {
    return this.id.replace("extra['", "").replace("']", "");
}).get().join();
alert(ids);
​

#1


4  

You can use wild card

你可以使用外卡

Live Demo

selectArray = $('[id^=extra]');

Iterating throuh all selects

通过所有选择迭代

selectArray.each(function() {
    alert(this.id);
})​

For getting the numbers in id of selects

用于获取选择ID的数字

Live Demo

selectArray = $('[id^=extra]');
ids = selectArray.map(function() {
    return this.id.replace("extra['", "").replace("']", "");
}).get().join();
alert(ids);
​