How do you select a particular option in a SELECT

2018-12-31 15:12发布

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

<div class="selDiv">
  <select class="opts">
    <option selected value="DEFAULT">Default</option>
    <option value="SEL1">Selection 1</option>
    <option value="SEL2">Selection 2</option>
  </select>
</div>

标签: jquery
19条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 15:40

By value, what worked for me with jQuery 1.7 was the below code, try this:

$('#id option[value=theOptionValue]').prop('selected', 'selected').change();
查看更多
像晚风撩人
3楼-- · 2018-12-31 15:41
/* This will reset your select box with "-- Please Select --"   */ 
    <script>
    $(document).ready(function() {
        $("#gate option[value='']").prop('selected', true);
    });
    </script>
查看更多
深知你不懂我心
4楼-- · 2018-12-31 15:42

You can just use val() method:

$('select').val('the_value');
查看更多
与风俱净
5楼-- · 2018-12-31 15:42

Using jquery-2.1.4, I found the following answer to work for me:

$('#MySelectionBox').val(123).change();

If you have a string value try the following:

$('#MySelectionBox').val("extra thing").change();

Other examples did not work for me so that's why I'm adding this answer.

I found the original answer at: https://forum.jquery.com/topic/how-to-dynamically-select-option-in-dropdown-menu

查看更多
余生无你
6楼-- · 2018-12-31 15:42

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');
查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 15:44

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

$row.find('#component').val('All');
查看更多
登录 后发表回答