這一個對我的作品,但它需要特殊字符處理一些調整:
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');
}
});
有一個[問題](https://github.com/harvesthq/chosen/issues/2593)在選擇github問題隊列關於這個問題。 – thirdender