admin管理员组文章数量:1395780
$("#name option").click(function(){ //some code })
HTML:
<select id = "name">
<optgroup label="label1">
<option>Option a</option>
<option>Option a</option>
</optgroup>
<optgroup label="label2">
<option>Option x</option>
<option>Option y</option>
</optgroup>
</select>
I need to trigger a function whenever the user chooses an option from the select list. The problem is that it works only on firefox but doesn't on safari nor chrome (don't 've IE to test on it).
What I seem to be missing is properly selecting the option tags within the select tag!!
$("#name option").click(function(){ //some code })
HTML:
<select id = "name">
<optgroup label="label1">
<option>Option a</option>
<option>Option a</option>
</optgroup>
<optgroup label="label2">
<option>Option x</option>
<option>Option y</option>
</optgroup>
</select>
I need to trigger a function whenever the user chooses an option from the select list. The problem is that it works only on firefox but doesn't on safari nor chrome (don't 've IE to test on it).
What I seem to be missing is properly selecting the option tags within the select tag!!
Share Improve this question asked Mar 16, 2012 at 17:23 Khaled MahmoudKhaled Mahmoud 3021 gold badge7 silver badges20 bronze badges 5- 2 you might want to try onchange of the select, instead of the onclick on the option. Some browsers don't fire all expected events on the option... – Nanne Commented Mar 16, 2012 at 17:25
-
1
Maybe the optgroup is messing up the selector. Does
$("#name optgroup option")
work ? – asawyer Commented Mar 16, 2012 at 17:25 - @Nanne Which should I use to trigger the onchange the option tags or the select tag?! using option tags doesn't work on safari!!! isn't that strange? – Khaled Mahmoud Commented Mar 16, 2012 at 17:28
- @asawyer adding opt group reverses the situation and it works only with firefox and not on safari – Khaled Mahmoud Commented Mar 16, 2012 at 17:34
- i think it was: onchange on select. – Nanne Commented Mar 16, 2012 at 23:00
3 Answers
Reset to default 8Try this instead:
$("#name").change(function(){ //some code });
You are marking it for having option directly under the entry IDed as name.
It should be:
$("#name optgroup option").click(function(){ //some code })
Also you might consider locking it down a little further by doing:
$("select#name optgroup option").click(function(){ //some code })
Check if you use css position:absolute. If you do see to that the z-index of the id or class is on top. You check this by assigning cursor:pointer; in the corresponding css id/class. If you se a hand (pointer) when mousing over, then you are on top. If not, the $('#myid').click function({}); will not work.
本文标签: javascript(quotid optionquot)click(function() ) doesn39t work with safari nor chromeStack Overflow
版权声明:本文标题:javascript - $("#id option").click(function(){ }) doesn't work with safari nor chrome? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744674281a2619015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论