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

javascript - onclick event in select HTML not working in Safari - Stack Overflow

programmeradmin4浏览0评论

I have an ASP.NET dropdown list control with onclick and onchange JavaScript events. Both work in IE, Mozilla, Opera and Chrome, but not in Safari. When I remove the onclick event, onchange suddenly works.

The reason I use onclick is to get the value of the dropdown list before it changes. Is there a way I can do that without using onclick? That is, get the value of the dropdown list before it changes/a new value is selected? I want to do this in JavaScript only.

I have an ASP.NET dropdown list control with onclick and onchange JavaScript events. Both work in IE, Mozilla, Opera and Chrome, but not in Safari. When I remove the onclick event, onchange suddenly works.

The reason I use onclick is to get the value of the dropdown list before it changes. Is there a way I can do that without using onclick? That is, get the value of the dropdown list before it changes/a new value is selected? I want to do this in JavaScript only.

Share Improve this question edited Feb 28, 2020 at 10:14 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 20, 2009 at 9:37 Jepe d HepeJepe d Hepe 9095 gold badges22 silver badges43 bronze badges 2
  • i've replaced the onclick event to onfocus. problem solved. i guess safari dont like onclick event on select element – Jepe d Hepe Commented Nov 20, 2009 at 9:45
  • Sorry to bump. For me, I found out that I was using closest which was not supported and that fixes it, after debugging with alert (no console.log on mobile ;)).. – Ismail Commented Apr 18, 2017 at 17:54
Add a ment  | 

5 Answers 5

Reset to default 7

Sometimes you need to add preference to the JavaScript function as "javascript:"; for example:

<select onclick="changeDate()">

should be

<select onclick="javascript:changeDate()">

Replacing the onclick event with onfocus solved the problem. I guess Safari doesn't like onclick event on select elements.

For me in Chrome, onclick working with second click on select box Where as in FF & IE, its working fine for first click itself.

<select onclick="changeDate()">

Should be changed to

<select onchange="changeDate()">

Safari seems to react as a open even and not a close event, like in the other browsers.

This issue can also be resolved by using the below code snippet:

<select onmousedown="if(event.button==0) changeDate()">

发布评论

评论列表(0)

  1. 暂无评论