Set select option 'selected', by value

后端 未结 28 2341
攒了一身酷
攒了一身酷 2020-11-22 10:40

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the

相关标签:
28条回答
  • 2020-11-22 11:22

    Try this. Simple yet effective javaScript + jQuery the lethal combo.

    SelectComponent :

    <select id="YourSelectComponentID">
                <option value="0">Apple</option>
                <option value="2">Banana</option>
                <option value="3">Cat</option>
                <option value="4">Dolphin</option>
    </select>
    

    Selection :

    document.getElementById("YourSelectComponentID").value = 4;
    

    Now your option 4 will be selected. You can do this, to select the values on start by default.

    $(function(){
       document.getElementById("YourSelectComponentID").value = 4;
    });
    

    or create a simple function put the line in it and call the function on anyEvent to select the option

    A mixture of jQuery + javaScript does the magic....

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

    You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue], so in your case you can select the option and set the selected attribute.

    $("div.id_100 > select > option[value=" + value + "]").prop("selected",true);
    

    Where value is the value you wish to select by.

    If you need to removed any prior selected values, as would be the case if this is used multiple times you'd need to change it slightly so as to first remove the selected attribute

    $("div.id_100 option:selected").prop("selected",false);
    $("div.id_100 option[value=" + value + "]")
            .prop("selected",true);
    
    0 讨论(0)
  • 2020-11-22 11:23

    An issue I ran into when the value is an ID and the text is a code. You cannot set the value using the code but you don't have direct access to the ID.

    var value;
    
    $("#selectorId > option").each(function () {
      if ("SOMECODE" === $(this).text()) {
        value = $(this).val();
      }
    });
    
    //Do work here
    
    0 讨论(0)
  • 2020-11-22 11:24

    Use the change() event after selecting the value. From the docs:

    If the field loses focus without the contents having changed, the event is not triggered. To trigger the event manually, apply .change() without arguments:

    $("#select_id").val("val2").change();
    

    More information is at .change().

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

    The easiest way to do that is:

    HTML

    <select name="dept">
       <option value="">Which department does this doctor belong to?</option>
       <option value="1">Orthopaedics</option>
       <option value="2">Pathology</option>
       <option value="3">ENT</option>
    </select>
    

    jQuery

    $('select[name="dept"]').val('3');
    

    Output: This will activate ENT.

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

    $('#graphtype option[value=""]').prop("selected", true);

    This works well where #graphtype is the id of the select tag.

    example select tag:

     <select name="site" id="site" class="form-control" onchange="getgraph1(this.options[this.selectedIndex].value);">
        <option value="" selected>Site</option> 
        <option value="sitea">SiteA</option>
        <option value="siteb">SiteB</option>
      </select>
    
    0 讨论(0)
提交回复
热议问题