How to catch second choice in <select> using jQuery?

The first click extends <option> ,

second click select one of them.

How to catch the second click?

+4
source share
4 answers

You can bind some events using jQuery one function as follows:

 $('select').one('click', firstClick); function firstClick(evt) { //do anything you might want to do $(evt.target).parent().one('click', secondClick); } function secondClick(evt) { //again, do you thing $(evt.target).parent().one('click', firstClick); } 

The one function will execute an event handler that you specify once, and then delete it. This solution allows you to switch the handler every time you click. Just be careful if you use this one. You also need to handle the blur event to reset the first handler.

You can also use the jQuery live function to minimize the number of event handlers that you create while preserving memory. It will also avoid some of the problems that I could have anticipated with my first solution.

 $('select option').live('click', selectHandler); function selectHandler(evt) { //insert magic here } 
+2
source

jQuery version of Pete's answer, which I think will satisfy the question asked

 $("option").click(function(){ alert($(this).text()); }); 

EDIT

Due to the above, which is not a cross browser, I did some searching and came across a message here about a stack overflow that might help.

Fire event every time DropDownList is selected using jQuery

+1
source

If the selection has changed, the onchange method is onchange .

0
source

You can always use onclick for each parameter in the combo box. This is not very, but it should work:

 <select> <option onclick="alert('May');">May</option> <option onclick="alert('June');">June</option> <option onclick="alert('July');">July</option> </select> 
0
source

All Articles