使用jQuery在引导的SelectPicker中搜索文本
我想在下拉列表中搜索文本,并将其设置为按文本选择。这是我的控件 HTML: 我正在使用引导选择器。我可以在代码中使用contain:还是selectpicker有自己的方法 更新:使用jQuery在引导的SelectPicker中搜索文本,jquery,twitter-bootstrap,bootstrap-select,Jquery,Twitter Bootstrap,Bootstrap Select,我想在下拉列表中搜索文本,并将其设置为按文本选择。这是我的控件 HTML: 我正在使用引导选择器。我可以在代码中使用contain:还是selectpicker有自己的方法 更新: 我尝试了简单的选择控制,它正在工作…这里是对于那些需要解决相同情况的人,我像这里一样解决了它 var userToSearchFor = "Jadaf"; $("#area_location_id").find("option:contains('" + userToSearchFor +"')") .each(fu
我尝试了简单的选择控制,它正在工作…这里是对于那些需要解决相同情况的人,我像这里一样解决了它
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');
下面的答案有一点变化
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值不能为st by attrselected,true
也可以通过选项值“不选择文本”来设置val。您到底想要什么?我想根据传递的变量显示所选文本。在上述情况下,我想让它显示所选的“Al Furjan”
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');
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');