事實證明我最終使用jquery自動完成組合框(http://jqueryui.com/demos/autocomplete/#combobox)進行了一些修改。具體來說,列出的change:函數會查找無效的選擇(如果用戶鍵入組合框,則是我們的情況)。下面是來自插件的默認代碼:
...
change: function(event, ui) {
if (!ui.item) {
...
});
if (!valid) {
// remove invalid value, as it didn't match anything
$(this).val("");
select.val("");
input.data("autocomplete").term = "";
return false;
}
}
}
...
,我把它改成:
...
change: function(event, ui) {
if (!ui.item) {
...
});
if (!valid) {
var _new_text = $(this).val()
var _new_option = '<option value="'+_new_text+'">' + _new_text + '</option>'
select.prepend(_new_option);//append to combobox
//now let's select it for submission
select.children("option").each(function(){
if($(this).text().match(_new_text)){
this.selected = true;
return false;
}
})
}
}
}
...
不知道是否有更好的方式來做到這一點,但是這是我在哪裏吧!現在正在對Rails中的內容進行過濾,但那是另一回事。