How do you select a particular option in a SELECT element in jQuery?

前端 未结 21 1707
Happy的楠姐
Happy的楠姐 2020-11-22 10:55

If you know the Index, Value or Text. also if you don\'t have an ID for a direct reference.

This, this and this are all helpful answers.

Example markup

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

    Try this

    you just use select field id instead of #id (ie.#select_name)

    instead of option value use your select option value

     <script>
        $(document).ready(function() {
    $("#id option[value='option value']").attr('selected',true);
       });
        </script>
    
    0 讨论(0)
  • 2020-11-22 11:28
       $(elem).find('option[value="' + value + '"]').attr("selected", "selected");
    
    0 讨论(0)
  • 2020-11-22 11:30

    You could name the select and use this:

    $("select[name='theNameYouChose']").find("option[value='theValueYouWantSelected']").attr("selected",true);
    

    It should select the option you want.

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

    The $('select').val('the_value'); looks the right solution and if you have data table rows then:

    $row.find('#component').val('All');
    
    0 讨论(0)
  • 2020-11-22 11:33

    For Jquery chosen if you send the attribute to function and need to update-select option

    $('#yourElement option[value="'+yourValue+'"]').attr('selected', 'selected');
    $('#editLocationCity').chosen().change();
    $('#editLocationCity').trigger('liszt:updated');
    
    0 讨论(0)
  • 2020-11-22 11:36

    i'll go with:-

    $("select#my-select option") .each(function() { this.selected = (this.text == myVal); });
    
    0 讨论(0)
提交回复
热议问题