1

我想在下拉列表中搜索文本并将其设置为由文本选择。这是我的控件

HTML:

  <select class="form-control input-sm selectpicker" id="area_location_id">
        <option value="" selected="">Select</option>
        <option value="17">Al Barsha</option>
       <option value="82">Al Furjan</option>
      <option value="4924">Al Garhoud</option>
      <option value="5787">Al Jadaf</option>
     <option value="5684">Al Mamzar</option
    </select>

查询:

 var mytxt = "Al Furjan";
("#area_location_id").find("option:contains('" + mytxt  + "')").each(function () {
    $(this).attr("selected", "selected");
});
$('#area_location_id').selectpicker('refresh');
$('#area_location_id').selectpicker('val', mytxt );

我正在使用引导选择器。我可以contain:在这段代码中使用还是 selectpicker 有自己的方法?

更新:

我尝试使用简单的选择控件,它正在工作......这是小提琴

4

2 回答 2

0

对于那些需要解决相同情况的人,我在这里解决了

var userToSearchFor = "Jadaf";
$("#area_location_id").find("option:contains('" + userToSearchFor +"')")
.each(function ()   {
 $(this).attr("selected", "selected");
});

$('#area_location_id').selectpicker('val', userToSearchFor );
$('#area_location_id').selectpicker('refresh');
于 2015-01-08T07:55:38.183 回答
0

对以下答案进行了一些更改。

var userToSearchFor = "text to search";
var selectedID;
$("#area_location_id").find("option:contains('" + userToSearchFor +"')")
    .each(function ()   {
        $(this).attr("selected", "selected");
        selectedID = $(this).val();
});

$('#area_location_id').selectpicker('val', selectedID );
$('#area_location_id').selectpicker('refresh');

请注意,selectpickers 的值不能是 attr("selected", true) 的 st,val 也可以通过选项值而不是选择文本来设置。

于 2020-12-16T22:14:58.370 回答