Tôi vừa triển khai tính năng này nhanh chóng và bẩn. Nó sử dụng một số biến toàn cầu, bạn có thể muốn cải thiện việc triển khai để loại bỏ các biến này.
Ở đây '#xsca_member_filter' là bộ lọc làm đầu vào văn bản và '#members' là đầu vào được chọn.
$('documenet').ready(function(){
init();
$('#xsca_member_filter').keyup(function(){
filter($(this));
});
});
//save all available options with their values and the empty option.
init = function(){
options = new Object();
$('#owner option').each(function(){
var obj = $(this);
if(obj.attr("value") != "")
options[obj.attr('value')] = obj.html();
else
emptyOption = obj.html();
});
selObj = $('#owner');
};
filter = function(elem){
var filter = elem.val();
var selected = $('#owner option:selected').val();
//delete all options and add the empty option
selObj.html("");
selObj.append("<option> "+emptyOption+" </option>");
//add all options conaining the filter string
for(value in options){
var option = options[value];
if((options[value]).indexOf(filter) != -1){
selObj.append("<option value='"+value+"'> "+options[value]+" </option>");
}
}
//select the previously selected option
$("#owner option[value = '"+selected+"']").prop("selected", true);
}
Đây chính xác là những gì tôi đã làm sau. Bạn có phiền nếu ai đó đóng gói này như là một plugin JQuery và phân phối nó? Tôi đang nghĩ đến việc phát hành một số plugin vào tháng tới và điều này sẽ tạo ra một plugin tốt. –
Đi cho nó. Có thể liên kết trở lại trang gốc trong nguồn, nếu điều đó ổn chứ? –
Chắc chắn, nếu nó được phát hành! Tôi đã ngồi trên một tải mã mà tôi giữ ý nghĩa để phát hành như JQuery/nguyên mẫu bổ sung fsince đầu năm nay. –