jquery 选择器以选择特定值的所有选择下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5151049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery selector to get all select dropdowns with a particular value selected
提问by ssahu
How do I select all the select boxes using a jQuery selector where value selected is "A1"?
如何使用 jQuery 选择器选择所有选择框,其中选择的值为“A1”?
Sample select box:
示例选择框:
<select name="accesslevel">
<option value="A1">A1 Access</option>
<option value="A2">A2 Access</option>
</select>
I have to use the selector to do the following thing:
我必须使用选择器来做以下事情:
.parent().nextAll('td[id^="A1Access"]').show();
.parent().nextAll('td[id^="A2Access"]').hide();
Can anyone help?
任何人都可以帮忙吗?
采纳答案by Prakash
Try :
尝试 :
$('select option[value="A1"]:selected')
回答by dsnettleton
As mentioned before, Prakash's solution is the closest, but it selects the option value rather than the select box itself. This can be fixed with the :has selector.
如前所述,Prakash 的解决方案是最接近的,但它选择选项值而不是选择框本身。这可以通过 :has 选择器修复。
$('select:has(option[value="A1"]:selected)')
回答by Mujah Maskey
$('select').filter(
function(){
if($(this).val()=="A1")
return true;
else
return false;
}
)
you can add your function after this like
您可以在此之后添加您的功能
$('select').filter(
function(){
if($(this).val()=="A1")
return true;
else
return false;
}
).parent().nextAll('td[id^="A1Access"]').show();
回答by Sandwich
Here's something like your original spec, but just modified a bit to work with my selectors.
这有点像你的原始规范,但只是修改了一点以与我的选择器一起使用。
$('.container').find('option[value=A1]').filter(':selected').parent('select').show();
And here it is Simplified:
这里是简化的:
$('option[value=A1]:selected').parent('select');
回答by Santosh Linkha
For simply selecting option in all html body
用于简单地在所有 html 正文中选择选项
alert($('option[value=A1]').html());
For select
为了 select
alert($('select[name=accesslevel]').find('option[value=A1]').html());
But alert will work with only one element.
但是 alert 只能处理一个元素。
For select you have
对于选择你有
$('option[value=A1]').parent().remove();
It will select the parent and remove it.
它将选择父项并将其删除。
回答by codersaif
Make an id like this..
做一个这样的id..
<select name="accesslevel" id="myselector">
<option value="A1">A1 Access</option>
<option value="A2">A2 Access</option>
</select>
Now select it with that id.
现在用那个 id 选择它。
$("select#myselector option:selected").each(function ()
{
alert($(this).text() + " ");
});