js:如何不区分大小写匹配

中国小小道 发布于 2014/09/24 13:28
阅读 5K+
收藏 1

这段代码在输入框输入小写的“baidu”能够出现下拉提示,但是输入框含有大写的字母就无法出现下拉框,如何在搜索框里输入“Baidu”,或者“BAIDU”都有下拉提示

<!DOCTYPE html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8"/>
<title>搜索提示</title>
<meta name=viewport content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<style type="text/css">body{font-family:"yaHei Consolas Hybrid";margin:0}ul{margin:0;list-style:none;padding:0}.search-block{width:400px;position:relative;margin:50px auto}.search-block input{font-size:20px}.lbl-default{position:absolute;cursor:text;top:15px;left:15px;color:#ccc}.txt-default{width:378px;height:28px;border:1px solid #ddd;padding:10px}.btn{border:0;width:100px;height:50px;background-color:#56bdf3;color:#fff;text-align:center;cursor:pointer}.search-button{position:absolute;left:300px;top:0}.search-advice{display:none;width:400px;position:absolute;background-color:#fff}#search-advice-list{border-left:1px solid #ddd;border-right:1px solid #ddd;border-bottom:1px solid #ddd}#search-advice-list li{display:block;width:378px;height:30px;padding:10px}#search-advice-list li:hover{background-color:#f5f5f5}</style>
<script type="text/javascript">var mockData=[{name:'baidu',link:"http://www.baidu.com"},{name:'baidu image',link:"http://image.baidu.com"}];function $(id){return document.getElementById(id);}
function hide(dom){dom.style.display='none';}
function show(dom){dom.style.display='block';}
function bindLabelToInput(labelId,inputId){$(inputId).onfocus=function(){hide($(labelId));};$(inputId).onblur=function(){if(this.value==null||this.value.length<1){show($(labelId));}};}
function createDomTo(tagName,arr,parentNode){for(var i=0;i<arr.length;i++){var tag=document.createElement(tagName);tag.innerHTML="<a href='"+arr[i].link+"' target=_black>"+arr[i].name+"</a>";parentNode.appendChild(tag);}}
window.onload=function(){bindLabelToInput('lbl-search','txt-search');var adviceWords=new Array();var ul=$('search-advice-list');$('txt-search').onkeyup=function(){if(this.value.length>0){for(var i=0;i<mockData.length;i++){if(mockData[i].name.indexOf(this.value)>=0){adviceWords.push(mockData[i]);}}
if(adviceWords.length>0){ul.innerHTML='';createDomTo('li',adviceWords,ul);show(ul.parentNode);}
adviceWords=new Array();}else{hide(ul.parentNode);}};};</script>
</head>
<body>
<div class=search-block>
<label for=txt-search id=lbl-search class=lbl-default>请输入要搜索的内容</label>
<input type=text id=txt-search class=txt-default />
<input type=button id=submit-button class="btn search-button" value="搜索"/>
<div class=search-advice>
<ul id=search-advice-list>
</ul>
</div>
</div>
</body>
</html>

加载中
0
lifer
lifer

1,发帖不负责任,这种无序代码一般人不会看

2,使用indexOf判断是否存在,是需要区分大小写的,你可以做一下处理,都格式化成小写比较或者大写比较。

if(mockData[i].name.indexOf(this.value)>=0)     {
      adviceWords.push(mockData[i]);
 }



lifer
lifer
回复 @中国小小道 : mockData[i].name.toLowerCase().indexOf(this.value.toLowerCase())
中国小小道
就是不知道如何区分大小写,其他的都实现了
0
Arrowing
Arrowing
请看对应插件的api文档。
中国小小道
这个还真没找到
0
山鸡Benki
山鸡Benki
实现大小需要写这么一大段js吗?
返回顶部
顶部