How to set selected option with selectpicker plugin from bootstrap

末鹿安然 提交于 2019-12-10 15:59:23

问题


I am using the Bootstrap-Select:

<select name="SelectAddress" class="selectpicker">
   <option value="a">Val 1</option>
   <option value="b">Val 2</option>
   <option value="c">Val 3</option>
   <option value="d">Val 4</option>
</select>

For example, I want to change Val 1 to Val 1.0 knowing Val 1 is selected. So I tried with no success:

$("select[name=SelectAddress]").text("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");

How can I achieve this?

Edit:

Found a solution, by inserting an ID on each <option>:

<select name="SelectAddress" class="selectpicker">
   <option id="1" value="a">Val 1</option>
   <option id="2" value="b">Val 2</option>
   <option id="3" value="c">Val 3</option>
   <option id="4" value="d">Val 4</option>
</select>

Then:

$("#1").html("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");

回答1:


You can do like this.

<select id="drop-down" name="SelectAddress" class="selectpicker">
   <option value="a">Val 1</option>
   <option value="b">Val 2</option>
   <option value="c">Val 3</option>
   <option value="d">Val 4</option>
</select>



$("#drop-down option[value='a']").text("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");


来源:https://stackoverflow.com/questions/40224287/how-to-set-selected-option-with-selectpicker-plugin-from-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!