做了一个简单的带搜索的下拉框,样子如下
大概代码如下:
1、存储初始化的select
var tempArr = new Array();
$("#selectoption").each(function () {
var node = [ $(this).text(), $(this).val() ];
tempArr.push(node);
});
2、select绑定查询
function selectTip() {
var txtObj = $('#cname').val();
var arr = [];
var selectHTML = $('#customerSpan').html().match(/<[^>]*>/)[0];
for (i = 0; i < tempArr.length; i++){
console.log(txtObj+'---'+tempArr[i][0]+'----'+tempArr[i][0].indexOf(txtObj));
if (tempArr[i][0].indexOf(txtObj) == 0){
arr[arr.length] = "<option value='" + tempArr[i][1] + "'>"
+ tempArr[i][0] + "</option>";
}
}
$('#customerSpan').html(selectHTML + arr.join() + "</SELECT>");
changeCustomerName();
}