admin管理员组文章数量:1405112
I want a function to be executed if a specific select option is visible. I have tried the following code:
if ($(("#daychoise option[value='monday2']").is(':visible'))) {
}
That didn't work and I got the following error in the console:
Uncaught TypeError: Object #daychoise option[value='monday2'] has no method 'is'
So how do I check if a select option is visible?
Thanks in advance
I want a function to be executed if a specific select option is visible. I have tried the following code:
if ($(("#daychoise option[value='monday2']").is(':visible'))) {
}
That didn't work and I got the following error in the console:
Uncaught TypeError: Object #daychoise option[value='monday2'] has no method 'is'
So how do I check if a select option is visible?
Thanks in advance
Share Improve this question asked Nov 29, 2013 at 7:41 HwendeHwende 6493 gold badges11 silver badges28 bronze badges 2-
You have extra
()
. Should be$("#daychoise option[value='monday2']").is(':visible')
. However it's not going to work anyway. – dfsq Commented Nov 29, 2013 at 7:43 - could you please make a fiddle ? we don't know what exactly you lookin' for – Pejman Commented Nov 29, 2013 at 7:53
6 Answers
Reset to default 8Wrong selector ! You should use this :
if ($("#daychoise option[value='monday2']").is(':visible')) {
}
You probably meant this
if (($("#daychoise option[value='monday2']").is(':visible'))) {
}
Note the location of the $
You can hav it like this
<select id="selectbox">
<option value="0">sunday</option>
<option value="1">monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<select>
and in jquery you can write as
<script>
$(document).ready(function(){
if($("#selectbox").val() == 1){
// do something
}
});
</script>
If you want to check if a value is selected in a multiple selectbox
<select multiple>...</select>
you should do it this way:
if ($.inArray('monday2', $("#daychoise").val()) !== -1) {
}
Please Try:
$(("#daychoise option[value='monday2']").change(function(){
if($(this).is(':visible')){
//do what you want
}
});
An option element always returns false when its status is tested with the :visible selector, as the element doesn't occupy space in the DOM. If you hide the option element with hide(), you can instead retrieve the CSS value for the display property:
$('#daychoise option[value="monday2"]').hide();
if($('#daychoise option[value="monday2"]').css('display') == 'none')
{
alert('monday2 is hidden');
}
else
{
alert('monday2 is visible');
}
本文标签: javascriptIf option value is visibleStack Overflow
版权声明:本文标题:javascript - If option value is visible - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744889599a2630691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论