js實現(xiàn)帶搜索功能的下拉框實時搜索實時匹配
更新時間:2013年11月05日 17:43:06 作者:
當select輸入框中每輸入一點內容的時候,在option中找出與內容匹配的選項顯示在option的前面選項中,下面有個不錯的示例,希望朋友們可以喜歡
1. 當select輸入框中每輸入一點內容的時候,在option中找出與內容匹配的選項顯示在option的前面選項中。
2. 如何獲取每次輸入的內容,當keyup的時候觸發(fā)函數(shù)。
問題:select標簽中可以輸入內容嗎?(解決:另一篇文章可選擇和輸入的下拉列表框 )
3. 如何獲得輸入框中的內容?(解決,在輸入框上添加onkeyup時間觸發(fā)的函數(shù)用js獲得)
4. 如何匹配?(解決)
4.1 如何獲得所有option中的內容?(解決)
function getSelectText()
{
//獲得所有select標簽
var object = document.getElementsByTagName('select');
//因為該html中只有一個select標簽,所以就是name = "aabb"代表的標簽
var obj = object[0];
//alert(obj.length);
//alert(obj[0]);
//保存所有option 的值
var allText;
for(i=0;i<obj.length;i++)
{
allText += obj[i].innerText+','; //關鍵是通過option對象的innerText屬性獲取到選項文本
}
return allText;
}
4.2 js分割字符串?(解決)
var allText = getSelectText();
//alert(allText);
// 每個option的內容分割開來
var eachOptin = new Array();
eachOptin=allText.split(","); //字符分割
4.3 如何在js中匹配?
//如果option內容中有輸入的內容就返回第一次匹配的位置(大于等于0),如果沒有匹配的就返回-1
var flag = eachOptin[i].indexOf(shuru) ;
5. 如何讓匹配的內容顯示在option的前面的選項?(通過改變option的index編號)( 解決)
方法:當查到匹配的選項的時候,將第一個option重新新增到select最后,然后,將第一個的值重置為匹配的option的值,然后刪掉原始匹配的option
7. js 實現(xiàn)select標簽右邊三角的功能(未解決,當搜索之后,直接顯示所有option選項可供選擇)
8.在匹配的option選項有多個的時候出現(xiàn)bedug,注意測試,和重新修改一下,應該是上面第五條中的邏輯問題
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測試</title>
<script>
function onku()
{
//獲得input輸入框的內容
var shuru = document.getElementById('ccdd').value;
var object = document.getElementsByTagName('select');
var obj = object[0];
//如果輸入的內容為空,所有的選項都匹配
if(shuru!= '')
{
//alert(shuru);
//獲得option中的所有內容
var allText = getSelectText();
//alert(allText);
// 每個option的內容分割開來
var eachOptin = new Array();
eachOptin=allText.split(","); //字符分割
var f = 1;
for (i=1;i<eachOptin.length-1 ;i++ )
{
//alert(eachOptin[i]);
//如果option內容中有輸入的內容就返回第一次匹配的位置(大于等于0),如果沒有匹配的就返回-1
var flag = eachOptin[i].indexOf(shuru) ;
if(flag >=0)
{
//alert(i);
//將index為f的option重新新增一遍,顯示在最后
obj.options.add(new Option(obj[i].innerText,obj[f].value));
//將編號為f的option重新賦值,賦值為符合條件的第一個option
obj.options[f]=new Option(eachOptin[i],eachOptin[i]);
//刪除最初存在的符合條件的option
obj.remove(i);
f++;
}
}
}
}
function getSelectText()
{
//獲得所有select標簽
var object = document.getElementsByTagName('select');
//因為該html中只有一個select標簽,所以就是name = "aabb"代表的標簽
var obj = object[0];
//alert(obj.length);
//alert(obj[0]);
//保存所有option 的值
var allText;
for(i=0;i<obj.length;i++)
{
//alert(obj[i].index);//獲得option的index編號
//alert(obj[i].value);//獲得option的value的值
allText+= obj[i].innerText+','; //關鍵是通過option對象的innerText屬性獲取到選項文本
}
return allText;
}
</script>
</head>
<body>
<span style=" position:absolute;border:1pt solid #c1c1c1; overflow:hidden;width:188px;height:19px;clip:rect(-1px 190px 190px 170px);">
<select name="aabb" id="aabb" style="width:190px;height:20px;margin:-2px;"
onChange="javascript:document.getElementById('ccdd').value=document.getElementById('aabb').options[document.getElementById('aabb').selectedIndex].value;">
<option value="" style="color:#c2c2c2;">--請選擇--</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="廣州">廣州</option>
<option value="上123">上123</option>
<option value="蘇州">蘇州</option>
</select>
</span>
<span style="position:absolute; border:1pt solid #c1c1c1;border-left:1pt solid #c1c1c1;border-bottom:1pt solid #c1c1c1;width:170px;height:19px;">
<input type="text" name="ccdd" id="ccdd" value="可選擇也可輸入的下拉框" style="width:170px;height:15px;border:0pt;" onkeyup="onku()">
</span>
</body>
</html>
注意代碼中的注釋
上面代碼的運行結果如下:
2. 如何獲取每次輸入的內容,當keyup的時候觸發(fā)函數(shù)。
問題:select標簽中可以輸入內容嗎?(解決:另一篇文章可選擇和輸入的下拉列表框 )
3. 如何獲得輸入框中的內容?(解決,在輸入框上添加onkeyup時間觸發(fā)的函數(shù)用js獲得)
4. 如何匹配?(解決)
4.1 如何獲得所有option中的內容?(解決)
復制代碼 代碼如下:
function getSelectText()
{
//獲得所有select標簽
var object = document.getElementsByTagName('select');
//因為該html中只有一個select標簽,所以就是name = "aabb"代表的標簽
var obj = object[0];
//alert(obj.length);
//alert(obj[0]);
//保存所有option 的值
var allText;
for(i=0;i<obj.length;i++)
{
allText += obj[i].innerText+','; //關鍵是通過option對象的innerText屬性獲取到選項文本
}
return allText;
}
4.2 js分割字符串?(解決)
復制代碼 代碼如下:
var allText = getSelectText();
//alert(allText);
// 每個option的內容分割開來
var eachOptin = new Array();
eachOptin=allText.split(","); //字符分割
4.3 如何在js中匹配?
復制代碼 代碼如下:
//如果option內容中有輸入的內容就返回第一次匹配的位置(大于等于0),如果沒有匹配的就返回-1
var flag = eachOptin[i].indexOf(shuru) ;
5. 如何讓匹配的內容顯示在option的前面的選項?(通過改變option的index編號)( 解決)
方法:當查到匹配的選項的時候,將第一個option重新新增到select最后,然后,將第一個的值重置為匹配的option的值,然后刪掉原始匹配的option
7. js 實現(xiàn)select標簽右邊三角的功能(未解決,當搜索之后,直接顯示所有option選項可供選擇)
8.在匹配的option選項有多個的時候出現(xiàn)bedug,注意測試,和重新修改一下,應該是上面第五條中的邏輯問題
代碼如下:
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測試</title>
<script>
function onku()
{
//獲得input輸入框的內容
var shuru = document.getElementById('ccdd').value;
var object = document.getElementsByTagName('select');
var obj = object[0];
//如果輸入的內容為空,所有的選項都匹配
if(shuru!= '')
{
//alert(shuru);
//獲得option中的所有內容
var allText = getSelectText();
//alert(allText);
// 每個option的內容分割開來
var eachOptin = new Array();
eachOptin=allText.split(","); //字符分割
var f = 1;
for (i=1;i<eachOptin.length-1 ;i++ )
{
//alert(eachOptin[i]);
//如果option內容中有輸入的內容就返回第一次匹配的位置(大于等于0),如果沒有匹配的就返回-1
var flag = eachOptin[i].indexOf(shuru) ;
if(flag >=0)
{
//alert(i);
//將index為f的option重新新增一遍,顯示在最后
obj.options.add(new Option(obj[i].innerText,obj[f].value));
//將編號為f的option重新賦值,賦值為符合條件的第一個option
obj.options[f]=new Option(eachOptin[i],eachOptin[i]);
//刪除最初存在的符合條件的option
obj.remove(i);
f++;
}
}
}
}
function getSelectText()
{
//獲得所有select標簽
var object = document.getElementsByTagName('select');
//因為該html中只有一個select標簽,所以就是name = "aabb"代表的標簽
var obj = object[0];
//alert(obj.length);
//alert(obj[0]);
//保存所有option 的值
var allText;
for(i=0;i<obj.length;i++)
{
//alert(obj[i].index);//獲得option的index編號
//alert(obj[i].value);//獲得option的value的值
allText+= obj[i].innerText+','; //關鍵是通過option對象的innerText屬性獲取到選項文本
}
return allText;
}
</script>
</head>
<body>
<span style=" position:absolute;border:1pt solid #c1c1c1; overflow:hidden;width:188px;height:19px;clip:rect(-1px 190px 190px 170px);">
<select name="aabb" id="aabb" style="width:190px;height:20px;margin:-2px;"
onChange="javascript:document.getElementById('ccdd').value=document.getElementById('aabb').options[document.getElementById('aabb').selectedIndex].value;">
<option value="" style="color:#c2c2c2;">--請選擇--</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="廣州">廣州</option>
<option value="上123">上123</option>
<option value="蘇州">蘇州</option>
</select>
</span>
<span style="position:absolute; border:1pt solid #c1c1c1;border-left:1pt solid #c1c1c1;border-bottom:1pt solid #c1c1c1;width:170px;height:19px;">
<input type="text" name="ccdd" id="ccdd" value="可選擇也可輸入的下拉框" style="width:170px;height:15px;border:0pt;" onkeyup="onku()">
</span>
</body>
</html>
注意代碼中的注釋
上面代碼的運行結果如下:
您可能感興趣的文章:
- angularJs-$http實現(xiàn)百度搜索時的動態(tài)下拉框示例
- jquery及原生js獲取select下拉框選中的值示例
- Vue.js 2.0中select級聯(lián)下拉框實例
- javascript實現(xiàn)省市區(qū)三級聯(lián)動下拉框菜單
- JS實現(xiàn)下拉框的動態(tài)添加(附效果)
- Extjs中ComboBoxTree實現(xiàn)的下拉框樹效果(自寫)
- 省市區(qū)三級聯(lián)動下拉框菜單javascript版
- javascript下拉框選項單擊事件的例子分享
- js實現(xiàn)select下拉框選擇
- js實現(xiàn)帶搜索功能的下拉框
相關文章
JS target與currentTarget區(qū)別說明
target在事件流的目標階段;currentTarget在事件流的捕獲,目標及冒泡階段。只有當事件流處在目標階段的時候,兩個的指向才是一樣的,而當處于捕獲和冒泡階段的時候,target指向被單擊的對象而currentTarget指向當前事件活動的對象(一般為父級)。2011-08-08
JavaScript中常用的字符串方法函數(shù)操作方法總結
這篇文章主要介紹了JavaScript中所有的字符串函數(shù)操作方法整理匯總,包括字符串的長度、連接、查找、截取、替換、分隔、轉換等處理方法,以及網(wǎng)址中獲取文件名等等,需要的朋友可以參考下2023-12-12
javascript+HTML5的canvas實現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼
這篇文章主要介紹了javascript+HTML5的canvas實現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼,使用了html5的canvas技術,可呈現(xiàn)出帶有3D效果的玫瑰花漸顯效果,非常逼真自然,需要的朋友可以參考下2015-08-08

