select 요소에서 javascript 검색을 만들었습니다.검색 및 제거 된 요소 제거
옵션 태그는 아무 것도 숨기거나 표시 할 CSS를 얻지 못합니다.이 솔루션을 위해 필적 할 수없는 옵션을 제거하고 재설정 목록 버튼에 대한 제거 된 옵션의 백업을 만듭니다.
잘 작동하지만 문제가 있습니다.이 선택 목록에 대해 약 19000 가지 옵션이 있습니다. 검색이 정상적으로 작동하지만 재설정 버튼을 누르면 19000에서 9500 옵션 만 되돌아옵니다.
감사합니다. 여기
코드입니다 : 내가 Y에서 관찰 한HTML
<h1>Search in select "option" tag</h1>
<select multiple name="selectMenu" id="selectMenu" style="width:100px" size=10>
<option value ="item 1">item 1</option>
<option value ="item 2">item 2</option>
<option value ="thing 3">thing 3</option>
<option value ="item 4">item 4</option>
<option value ="stuff 5">stuff 5</option>
<option value ="stuff 6">stuff 6</option>
<option value ="stuff 7">stuff 7</option>
<option value ="item 8">item 8</option>
</select>
<p>Search within this list</p>
<input type=text name="search" id="search" onkeypress="searchItems();">
<br>
<input type=button value="Search" onclick="searchItems();">
<input type=button value="Reset List" onclick="resetList();">
자바 스크립트
var itemList = null;
var itemListOriginal = new Array();
var backup = false;
function searchItems() {
itemList = document.getElementById("selectMenu");
var searchStrObj = document.getElementById("search");
var itemDescription = "";
// replace white space with wild card
var searchString = searchStrObj.value;
searchString = searchString.replace(" ", ".*");
var re = new RegExp("(" + searchString + ")", "i"); //"i" sets "ignore case" flag
if (itemListOriginal.length < 1)
backup = true;
else
backup = false;
// loop through options and check for matches
for (i=itemList.options.length - 1; i >=0 ; i--) {
itemDescription = itemList.options.item(i).text;
if (backup) {
hash = new Array();
hash['name'] = itemDescription;
hash['value'] = itemList.options.item(i).value;
itemListOriginal[i] = hash;
}
if (!itemDescription.match(re)) {
itemList.remove(i);
}
}
return false;
}
function resetList() {
//hack! remove all elements from list before repopulating
for (i=itemList.options.length - 1; i >=0 ; i--) {
itemList.remove(i);
}
for (i=0; i < itemListOriginal.length; i++) {
hash = itemListOriginal[i];
//option = new Option(hash['name'], hash['value']); REMOVED
//itemList.options.add(option, i); REMOVED
itemList.options[i] = new Option(hash['name'], hash['value'], false, false);
}
document.getElementById("search").value = "";
}
다음 번부터 링크를 첨부하지 말고 코드를 여기에 포함하십시오. –
죄송합니다. 긴 코드 였고 일부 코드가 숨어 있었기 때문에 여기에 추가하기가 어려웠습니다. –
네, 제발! 나는 다른 것을 편집하고 있기 때문에 그것을 위해 추가했습니다. –