简体   繁体   中英

click option in select by jquery?

<select id="myselect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

Whenever an option in the select is clicked, I need to call an ajax with $("#myselect option").click(hanler) event handler.But this feature is not working in Safari Browser

ex:

$("#myselect option").click(function(){
    // call ajax
})

Try using change() while selecting option from select.

$("#myselect option").on("change",function(){
// call ajax
});

You can look into the answer already written here . Give a try by adding an extra event like blur();

$('select').quickChange(function () {
  $(this).blur();
  $('select').focus(); // somehow prevents an extra blur from firing   on focus
});


$("#select-choice-month").change(function () {
  // update second select dynamically
});

Hope it will give you an answer.

Its better to use change event rather than click event, also bind the code inside $(document).ready . Please try the sample code and verify whether this fits for you.

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $(document).ready(function () { $("select#myselect").change(function () { console.log("You have selected " + $("select#myselect").val()); //Call your ajax here. }) }); </script> </head> <body> <select id="myselect"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </body> </html> 

You need to use the "change" event on the select tag, not the options themself.

$('#myselect').on('change', function (e) {
    myFunction();
});

Everytime, you change the current value of your select, "myfunction' will be call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM