I have select:
我选择了:
<select>
<option>Option1</option>
<option selected>Option2</option>
<option>Option3</option>
</select>
How Can I know number of selected element?
I use:
我如何知道所选元素的数量?我用:
var shop_val = $("select option").eq();
but its return object, I want number of elements and each item's Value.
但它的返回对象,我想要多个元素和每个项目的值。
6 个解决方案
#1
3
$("select option:selected").index()+1 gives the number
#2
3
you can do this
你可以这样做
var shop_val = $("select option:selected").length();
you can do like this also
你也可以这样做
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
alert(str);
})
#3
2
- add value attributes to the options
- 为选项添加值属性
$('select').val();
- $( '选择')VAL()。
#4
1
You can use the :selected selector to select the selected items, and then get the .length, like this:
您可以使用:selected选择器选择所选项目,然后获取.length,如下所示:
var sel = $("#mySelect :selected").length;
#5
1
I'd suggest:
我建议:
var shop_val = $("select option:selected").index();
#6
1
Add value attribute as
添加值属性为
<select>
<option value=1>Option1</option>
<option selected value=2>Option2</option>
<option value=3>Option3</option>
</select>
and then use
然后使用
$('select').val();
$( '选择')VAL()。
#1
3
$("select option:selected").index()+1 gives the number
#2
3
you can do this
你可以这样做
var shop_val = $("select option:selected").length();
you can do like this also
你也可以这样做
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
alert(str);
})
#3
2
- add value attributes to the options
- 为选项添加值属性
$('select').val();
- $( '选择')VAL()。
#4
1
You can use the :selected selector to select the selected items, and then get the .length, like this:
您可以使用:selected选择器选择所选项目,然后获取.length,如下所示:
var sel = $("#mySelect :selected").length;
#5
1
I'd suggest:
我建议:
var shop_val = $("select option:selected").index();
#6
1
Add value attribute as
添加值属性为
<select>
<option value=1>Option1</option>
<option selected value=2>Option2</option>
<option value=3>Option3</option>
</select>
and then use
然后使用
$('select').val();
$( '选择')VAL()。