有没有办法让搜索框无效,但仍然有下拉列表选择按键上的选项,这是启用搜索时的默认行为?
$(element).chosen({ disable_search: true });
我尝试使用以下方式启用搜索并隐藏搜索框(没有运气):
$('ul.chosen-choices li.search-field').hide();
似乎需要显示搜索才能使按键像普通选择一样工作。有人有运气吗?提前谢谢!
答案 0 :(得分:2)
这个对我有用,但需要对特殊字符处理进行一些调整:
var originalObjectSelector = "select#yourSelectId";
var $container = $(originalObjectSelector).data("chosen").container
var $orig=$(originalObjectSelector);
$container.bind("keypress", function(event){
var charCode = event.charCode;
var keyCode = event.keyCode;
var character = String.fromCharCode(charCode);
//console.log("kc:", keyCode, "cc", charCode, "char:", character);
//console.log(event);
if(keyCode==13){
//enter is pressed;
$orig.trigger('change');
}
if(charCode != 0 && character.match(/^[-_ a-zA-Z0-9]+$/))
{
//console.log('alphanumeric or space');
$orig.find("option").removeAttr("selected")
.filter(function() { return $(this).text().toLowerCase().indexOf(character.toLowerCase()) === 0; })
.first().attr("selected", "selected");
$orig.trigger('chosen:updated');
}
});