admin管理员组文章数量:1301512
I have the following snippet of a HTML select box:
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0" selected="">abc</option>
<option att="2ndOption" value="1">def</option>
When I run the following pieces of code:
var attribute = $('#Obj_cboscreen :selected').attr('att');
var name = $('#Obj_cboscreen :selected').html();
I get 1stOption and abc (respectively).
When I change the select box to a different option, for example:
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0">abc</option>
<option att="2ndOption" value="1" selected="">def</option>
I get the same results of the output before.
"1stOption and abc"
Now I noticed the selected="". That doesn't look right, I've looked around in the project and I'm unable to find where that is being produced, but it is rendering properly.
I'm wondering why the selected selector isn't working properly?
I have the following snippet of a HTML select box:
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0" selected="">abc</option>
<option att="2ndOption" value="1">def</option>
When I run the following pieces of code:
var attribute = $('#Obj_cboscreen :selected').attr('att');
var name = $('#Obj_cboscreen :selected').html();
I get 1stOption and abc (respectively).
When I change the select box to a different option, for example:
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0">abc</option>
<option att="2ndOption" value="1" selected="">def</option>
I get the same results of the output before.
"1stOption and abc"
Now I noticed the selected="". That doesn't look right, I've looked around in the project and I'm unable to find where that is being produced, but it is rendering properly.
I'm wondering why the selected selector isn't working properly?
Share edited Aug 12, 2011 at 22:42 webdad3 asked Aug 12, 2011 at 22:26 webdad3webdad3 9,08031 gold badges128 silver badges228 bronze badges 3-
Your code has errors:
att
is undefined, you are missing a space in your selector before:selected
, and you are missing an end tag for your<select>
. But running your code with those errors in place or with those errors fixed does not reproduce the behavior you describe in either case. jsfiddle/tg9QF Works for me. Please post code that actually reproduces your bug - post a jsFiddle. – gilly3 Commented Aug 12, 2011 at 22:47 - I originally transposed my code incorrectly and I have since editted it. Also, I stated that it was a snippet of code so that is why I didn't include the end tag for the select. This code is built dynamically via a COM object so I'm not sure even if I could do a fiddle that it would work correctly. I am going to have to do more research into alternatives. – webdad3 Commented Aug 12, 2011 at 22:51
- 1 View source, copy/paste, trim irrelevent code, paste in jsFiddle. The trim irrelevant code step is the tedious one, but in my experience, I usually find my solution through that process anyway. – gilly3 Commented Aug 12, 2011 at 23:00
4 Answers
Reset to default 4The following code works for me -
var attribute = $('#Obj_cboscreen :selected').attr("att");
var name = $('#Obj_cboscreen :selected').html();
alert(attribute + name);
There are a couple of changes from your code.
attr(att)
to attr("att")
$('#Obj_cboscreen:selected')
to $('#Obj_cboscreen :selected')
The following jsfiddle shows the code working when the 'selected' attribute is added to the second option.
http://jsfiddle/ipr101/bjWt3/
There is a problem with att, i think this should be enclosed in quotes, Like
var attribute = $('#Obj_cboscreen option:selected').attr("att");
True is missing in selected, always format your html, because some times its behave unmonly
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0">abc</option>
<option att="2ndOption" value="1" selected="true">def</option>
Here take a look at this -- maybe it will help you out:
http://jsfiddle/dKyrM/
var attribute = $('#Obj_cboscreen option[selected=true]').attr("att");
alert(attribute);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="Obj_cboscreen" class="menubarselect" name="page">
<option att="1stOption" value="0">abc</option>
<option att="2ndOption" value="1" selected="true">def</option>
本文标签: javascriptjquery selected not working as expectedStack Overflow
版权声明:本文标题:javascript - jquery :selected not working as expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678705a2392039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论