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?
有人可以帮忙吗?
6 个解决方案
#1
18
Try :
试试:
$('select option[value="A1"]:selected')
#2
39
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)')
#3
3
$('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();
#4
2
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');
#5
0
For simply selecting option in all html body
只需在所有html正文中选择选项
alert($('option[value=A1]').html());
For select
如需选择
alert($('select[name=accesslevel]').find('option[value=A1]').html());
But alert will work with only one element.
但警报仅适用于一个元素。
For select you have
如果您有选择
$('option[value=A1]').parent().remove();
It will select the parent and remove it.
它将选择父项并将其删除。
#6
0
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() + " ");
});
#1
18
Try :
试试:
$('select option[value="A1"]:selected')
#2
39
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)')
#3
3
$('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();
#4
2
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');
#5
0
For simply selecting option in all html body
只需在所有html正文中选择选项
alert($('option[value=A1]').html());
For select
如需选择
alert($('select[name=accesslevel]').find('option[value=A1]').html());
But alert will work with only one element.
但警报仅适用于一个元素。
For select you have
如果您有选择
$('option[value=A1]').parent().remove();
It will select the parent and remove it.
它将选择父项并将其删除。
#6
0
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() + " ");
});