如果我只知道文字而不是值,我怎样才能更改下拉列表?
这是我的选择 -
<select id="app_id" class="selectpicker">
<option value="">--Select--</option>
<option value="1">Application 1</option>
<option value="2">Application 2</option>
</select>
答案 0 :(得分:10)
您可以使用jquery filter()和prop(),如下所示
jQuery("#app_id option").filter(function(){
return $.trim($(this).text()) == 'Application 2'
}).prop('selected', true);
<强> DEMO 强>
使用 BOOTSTRAP SELECT ,
jQuery("#app_id option").filter(function(){
return $.trim($(this).text()) == 'Application 2'
}).prop('selected', true);
$('#app_id').selectpicker('refresh');
<强> DEMO 强>
答案 1 :(得分:0)
试试这个:
$(function(){
// Init selectpicker
$('#app_id').selectpicker({
style: 'btn-info',
size: 4
});
// Set desired text
var optionToSet = "Application 1";
$("#app_id option").filter(function(){
// Get the option by its text
var hasText = $.trim($(this).text()) == optionToSet;
if(hasText){
// Set the "selected" value of the <select>.
$("#app_id").val($(this).val());
// Force a refresh.
$("#app_id").selectpicker('refresh')
}
});
});
受@ Sathish的回答启发