admin管理员组文章数量:1277899
Someone helped me with this earlier, but there is one problem and I already closed that question. I do not want to use JQuery
. The following code works, it allows you to search a dropdown menu:
<html>
<head>
<script type="text/javascript">
function searchSel()
{
var input = document.getElementById('realtxt').value.toLowerCase(),
len = input.length,
output = document.getElementById('realitems').options;
for(var i=0; i<output.length; i++)
if (output[i].text.toLowerCase().slice(0, len) == input)
output[i].selected = true;
if (input == '')
output[0].selected = true;
}
</script>
</head>
<body>
<form>
search <input type="text" id="realtxt" onkeyup="searchSel()">
<select id="realitems">
<option value="">select...</option>
<option value="1">Power hungry</option>
<option value="2">Super man</option>
<option value="3">Hyperactive</option>
<option value="4">Bored</option>
<option value="5">Human</option>
</select>
</form>
</body>
</html>
Here is the problem.
It is only limited to the matching first letters of the first word. So if you typed in Super
for Super Man, it would work. But if you typing with Man
it will not show. Is there a way to make it match the whole string for a matching value? Thanks.
Someone helped me with this earlier, but there is one problem and I already closed that question. I do not want to use JQuery
. The following code works, it allows you to search a dropdown menu:
<html>
<head>
<script type="text/javascript">
function searchSel()
{
var input = document.getElementById('realtxt').value.toLowerCase(),
len = input.length,
output = document.getElementById('realitems').options;
for(var i=0; i<output.length; i++)
if (output[i].text.toLowerCase().slice(0, len) == input)
output[i].selected = true;
if (input == '')
output[0].selected = true;
}
</script>
</head>
<body>
<form>
search <input type="text" id="realtxt" onkeyup="searchSel()">
<select id="realitems">
<option value="">select...</option>
<option value="1">Power hungry</option>
<option value="2">Super man</option>
<option value="3">Hyperactive</option>
<option value="4">Bored</option>
<option value="5">Human</option>
</select>
</form>
</body>
</html>
Here is the problem.
It is only limited to the matching first letters of the first word. So if you typed in Super
for Super Man, it would work. But if you typing with Man
it will not show. Is there a way to make it match the whole string for a matching value? Thanks.
1 Answer
Reset to default 8I have created a fiddle please check. http://jsfiddle/wLzs4/3/
I have used the indexOf javascript function for your case.
function searchSel()
{
var input = document.getElementById('realtxt').value.toLowerCase();
len = input.length;
output = document.getElementById('realitems').options;
for(var i=0; i<output.length; i++)
if (output[i].text.toLowerCase().indexOf(input) != -1 ){
output[i].selected = true;
break;
}
if (input == '')
output[0].selected = true;
}
本文标签: javascriptWorking code to search within dropdown menuStack Overflow
版权声明:本文标题:javascript - Working code to search within dropdown menu - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741231490a2362190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论