Click event on select option element in chrome

前端 未结 13 949
闹比i
闹比i 2020-11-22 11:38

I\'m having a problem in Chrome with the following:

var items = $(\"option\", obj);  

items.each(function(){

    $(this).click(function(){

           


        
相关标签:
13条回答
  • 2020-11-22 12:01

    Maybe one of the new jquery versions supports the click event on options. It worked for me:

    $(document).on("click","select option",function() {
      console.log("nice to meet you, console ;-)");
    });
    

    UPDATE: A possible usecase could be the following: A user sends a html form and the values are inserted into a database. However one or more values are set by default and you flag this automated entries. You also show the user that his entry is generated automatically, but if he confirm the entry by clicking on the already selected option you change the flag in the database. A rare sue case, but possible...

    0 讨论(0)
  • 2020-11-22 12:08

    What usually works for me is to first change the value of the dropdown, e.g.

    $('#selectorForOption').attr('selected','selected')

    and then trigger the a change

    $('#selectorForOption').changed()

    This way, any javascript that is wired to

    0 讨论(0)
  • 2020-11-22 12:12

    I don't believe the click event is valid on options. It is valid, however, on select elements. Give this a try:

    $("select#yourSelect").change(function(){
        process($(this).children(":selected").html());
    });
    
    0 讨论(0)
  • 2020-11-22 12:12

    We can achieve this other way despite of directly calling event with <select>.

    JS part:

    $("#sort").change(function(){
    
        alert('Selected value: ' + $(this).val());
    });
    

    HTML part:

    <select id="sort">
        <option value="1">View All</option>
        <option value="2">Ready for Review</option>
        <option value="3">Registration Date</option>
        <option value="4">Last Modified</option>
        <option value="5">Ranking</option>
        <option value="6">Reviewed</option>
    </select>
    
    0 讨论(0)
  • 2020-11-22 12:16
    <select id="myselect">
        <option value="0">sometext</option>
        <option value="2">Ready for Review</option>
        <option value="3">Registration Date</option>
    </select>
    
    $('#myselect').change(function() {
        if($('#myselect option:selected').val() == 0) {
        ...
        }
        else {
        ...
        }
    });
    
    0 讨论(0)
  • 2020-11-22 12:17

    Looking for this on 2018. Click event on option tag, inside a select tag, is not fired on Chrome.

    Use change event, and capture the selected option:

    $(document).delegate("select", "change", function() {
        //capture the option
        var $target = $("option:selected",$(this));
    });
    

    Be aware that $target may be a collection of objects if the select tag is multiple.

    0 讨论(0)
提交回复
热议问题