例如,
<div id="wrapper">
<select>
<option value="en" selected="selected">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
<select>
<option value="en" >english</option>
<option value="ar">العربية</option>
<option value="az" selected="selected">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
<select>
<option value="en" >english</option>
<option value="ar">العربية</option>
<option value="az" selected="selected">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
</div>
如何选择带有'select'
value="az"
代码
我找到了唯一的方法:
var val_ = "az";
var selects = $div.find("select").filter(function(index_){
var val0 = $(this).val();
return val0 == val_;
});
有更优雅的方式吗?
编辑。我的问题是关于'select'标签。 stackoverflow中的所有问题都与子'选项'标签有关。
答案 0 :(得分:2)
[attribute='value']
来选择使用特定值:has()
选择器检查所选的选项值:selected
选择器以获取所选选项<强> CODE:强>
$('select:has(:selected[value="az"])')
使用变量你可以
var val_ = "az";
var selects = $('select:has(:selected[value="' + val_ + '"])');
例如:
var val_ = "az";
$('select').change(function() {
var selects = $('select:has(:selected[value="' + val_ + '"])');
console.log(selects.length);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="wrapper">
<select>
<option value="en" selected="selected">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
<select>
<option value="en">english</option>
<option value="ar">العربية</option>
<option value="az" selected="selected">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
<select>
<option value="en">english</option>
<option value="ar">العربية</option>
<option value="az" selected="selected">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
</div>
&#13;
答案 1 :(得分:0)
您可以选择一个或多个<select>
元素,其中包含值为“az”的<option>
,如下所示:
$("select option[value='az']")