Set selected option of select box

2019-01-03 23:03发布

I want to set a option that was selected previously to be displayed on page load. I tried it with the following code:

$("#gate").val('Gateway 2');

with

<select id="gate">
    <option value='null'>- choose -</option>
    <option value='Gateway 1'>Gateway 1</option>
    <option value='Gateway 2'>Gateway 2</option>
</select>

But this does not work. Any ideas?

14条回答
叼着烟拽天下
2楼-- · 2019-01-04 00:01
$(document).ready(function() {
    $("#gate option[value='Gateway 2']").prop('selected', true);
    // you need to specify id of combo to set right combo, if more than one combo
});
查看更多
贼婆χ
3楼-- · 2019-01-04 00:01
  $("#form-field").val("5").trigger("change");
查看更多
登录 后发表回答