问题
I am using onclick
event in option tag for select
box
<select>
<option onclick="check()">one</option>
<option onclick="check()">two</option>
<option onclick="check()">three</option>
</select>`
onclick
event is not working on IE and Chrome but it is working fine in firefox,
here I don't want to use onchange
event on select tag bcz it will not trigger an event if user selects same option again
Eg:say first time user selects "one" dropdown I will open a popup after processing some stuff user closes the popup,suppose if user wants to select same "one" dropdown it will not trigger any event.this can be solved using onclick event on option tag but its not working on IE and chrome
Is there any work around for this ?
回答1:
onclick
event on option
tag will fail on most versions of IE, Safari and Chrome: reference
If you want to trigger an event whenever user select, why not simply use:
<select onclick="check()">
<option>one</option>
<option>two</option>
<option>three</option>
And if you want to do something with the specific option user selected:
<select onclick="if (typeof(this.selectedIndex) != 'undefined') check(this.selectedIndex)">
<option>one</option>
<option>two</option>
<option>three</option>
This way you are guaranteed to call check()
if and only if an option is selected.
Edit: As @user422543 pointed out in the comments, this solution will not work in Firefox. I therefore asked another question here: Why does Firefox react differently from Webkit and IE to "click" event on "select" tag?
So far it seems using <select>
tag is will not work consistently in all browsers. However, if we simulate a select menu using library such as jQuery UI select menu or Chosen to create a select menu instead of using <select>
tag, click
event will be fired on <ul>
or <li>
tag which is consistent in all browsers I tested.
回答2:
I have another suggestion, it's not 100%, but almost:
<select onchange="valueChanged(this.value); this.selectedindex = -1">
<option style="display: none"></option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
</select>
This way the event will be triggered even if the user selected the same option twice. The hitch is that IE will display the empty option (it ignores the style attribute), but clicking it will not fire the event, since it always starts out as being selected, and therefore selecting it does not trigger onchange...
回答3:
This emulates an onclick
event on your option
s by recording the number of clicks.
http://jsfiddle.net/yT6Y5/1/
- The first click is ignored (used to expand your dropdown),
- The second click is effectively your "selection". (The click count is then reset).
It doesn't cater for keyboard interaction though....
Oh and I'm making use of JQuery, but you could re-do it using pure JS
回答4:
You just have to
- put the script above the select,
- set onclick and onblur for select as shown in code
- and customize the check function.
I have tested it and it works :).
<script>
selectHandler = {
clickCount : 0,
action : function(select)
{
selectHandler.clickCount++;
if(selectHandler.clickCount%2 == 0)
{
selectedValue = select.options[select.selectedIndex].value;
selectHandler.check(selectedValue);
}
},
blur : function() // needed for proper behaviour
{
if(selectHandler.clickCount%2 != 0)
{
selectHandler.clickCount--;
}
},
check : function(value)
{
// you can customize this
alert('Changed! -> ' + value);
}
}
</script>
<select onclick="selectHandler.action(this)" onblur="selectHandler.blur()">
<option value="value-1"> 1 </option>
<option value="value-2"> 2 </option>
<option value="value-3"> 3 </option>
<option value="value-4"> 4 </option>
</select>
回答5:
I found that the following worked for me - instead on using on click, use on change e.g.:
jQuery('#element select').on('change', (function() {
//your code here
}));
回答6:
Use function(this)
in id of select tag
for example
<script type="application/javascript">
var timesSelectClicked = 0;
$(function() {
$(document).on('click keypress', 'select', function (e) {
if (timesSelectClicked == 0)
{
timesSelectClicked += 1;
}
else if (timesSelectClicked == 1)
{
timesSelectClicked = 0;
prompt($('select option:selected').text());
}
});
});
</script>
回答7:
This code is not working in Google Chrome but is working in Mozilla.
<select>
<option value="2" (click)="myFunction($event)">
<div>2</div>
</option>
<option value="5" (click)="myFunction($event)">
<div>5</div>
</option>
<option value="10" (click)="myFunction($event)">
<div>10</div>
</option>
</select>
This solition is working in Chrome and Mozilla. I didn't test in Safari.;
<select (change)="myFunction($event)">
<option value="2">
<div>2</div>
</option>
<option value="5">
<div>5</div>
</option>
<option value="10">
<div>10</div>
</option>
</select>
来源:https://stackoverflow.com/questions/9972280/onclick-on-option-tag-not-working-on-ie-and-chrome