使用jQuery在Bootstrap的SelectPicker中搜索文本

时间:2015-01-07 13:16:35

标签: jquery twitter-bootstrap bootstrap-select

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

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>

Jquery的:

 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 );

我正在使用bootstrap selectpicker。我可以在此代码中使用contain:,还是selectpicker有自己的方法?

更新:

我尝试使用简单的选择控件,它正在运行......这是Fiddle

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

下面的答案有一点改变。

(
    df1.merge(
        df2.assign(agent_id_right=df2.agent_id.astype("Int64")),
        on="agent_id",
        how="left",
        suffixes=("", "_right"),
    )
    .query("created_at_email >= created_at")
    .sort_values("created_at", ascending=False)
    .assign(counter=lambda x: x.groupby(list(df1.columns), sort=False).cumcount())
    .loc[
        lambda x: x.counter.lt(4),
        [
            "created_at_email",
            "created_at",
            "agent_id",
            "agent_id_right",
            "unique_col",
            "unique_col_right",
        ],
    ]
    .sort_index(ignore_index=True)
)


  created_at_email  created_at  agent_id  agent_id_right    unique_col  unique_col_right
0   2020-01-05     2020-01-04      1           1             a             a
1   2020-01-05     2020-01-05      1           1             a             b
2   2020-01-05     2020-01-03      1           1             a             f
3   2020-01-05     2020-01-04      1           1             a             g
4   2020-01-08     2020-01-06      3           3             c             c
5   2020-01-03     2020-01-02      1           1             d             e
6   2020-01-03     2020-01-03      1           1             d             f

请注意,attpickers值不能通过attr(“ selected”,true)进行st 也可以通过选项值设置val而不选择文本。