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

时间:2012-11-23 07:01:32

标签: jquery arrays

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

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

在php中,我可以使用:

获取值
$_REQUEST['extra']

我得到一个数组。

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

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

1 个答案:

答案 0 :(得分:4)

您可以使用通配符

<强> Live Demo

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

通过所有选择迭代

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

获取选择ID的数字

<强> Live Demo

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