最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $("#id option").click(function(){ }) doesn't work with safari nor chrome? - Stack Overflo

programmeradmin2浏览0评论
$("#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
Add a ment  | 

3 Answers 3

Reset to default 8

Try 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.

发布评论

评论列表(0)

  1. 暂无评论