javascript 类型错误:name.toLowerCase 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16371382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: name.toLowerCase is not a function
提问by SREE ANI
I am creating some auto-complete code to list countries from a JSON array. It works fine, but with an error in console. I don't know when it will be harmful. Please can you help to solve this?
我正在创建一些自动完成代码来从 JSON 数组中列出国家/地区。它工作正常,但在控制台中出现错误。不知道什么时候会有害。请你能帮忙解决这个问题吗?
CSS
CSS
.mainpart {
width:100%;
height:auto;
}
.first {
width:150px;
height:25px;
float:left
}
.second {
width:150px;
height:25px;
float:left
}
ul, li {
list-style:none;
}
.li-autoList {
background-color:#CBCACC;
border:1px solid #fff;
cursor:pointer;
}
HTML
HTML
<div class="mainpart">
<div class="first">
<input type="text" id="county" onclick="autoList('#county','country')" />
</div>
<div class="second">
<input type="text" id="county2" onclick="autoList('#county2','city')" />
</div>
</div>
JS
JS
<script type="text/javascript">
function autoSort(id,list){
if(list=='country'){
county=(co.response.data);
}else{
county=(ci.response.data);
}
li='';
for(i=0;i<county.length;i++){
c=$(id).val().toUpperCase().replace(/["'\(\)]/g, "");
conDec=county[i].description+'('+county[i].cod+')';
conDec=conDec.toUpperCase().replace(/["'\(\)]/g, "");
n=(conDec.search(c));
if(n>=0){
cname=county[i].description+'('+county[i].cod+')';
li+='<li class="li-autoList"><input type="checkbox" class="itm" value="'+county[i].description+'('+county[i].cod+')">'+county[i].description+'</li>';
}
}
$('.li-autoList').remove();
$(id).after(li);
}
function autoList(id,list){
if(list=='country'){
county=(co.response.data);
}else{
county=(ci.response.data);
}
li='';
for(i=0;i<county.length;i++){
c=$(id).val().toUpperCase().replace(/["'\(\)]/g, "");
conDec=county[i].description+'('+county[i].cod+')';
conDec=conDec.toUpperCase().replace(/["'\(\)]/g, "");
n=(conDec.search(c));
chk='';
if(conDec==c){
chk='checked';
}
cname=county[i].description+'('+county[i].cod+')';
li+='<li class="li-autoList liItm"><input type="checkbox" '+chk+' class="itm" value="'+county[i].description+'('+county[i].cod+')">'+county[i].description+'</li>';
}
$('.li-autoList').remove();
$(id).after(li);
$('.itm').click(function(){ $(id).val( $(this).val());
$('.li-autoList').remove();
// autoList(id,list);
});
$('.liItm').click(function(){
// $(id).val( $(this).val());
var cb = $(this).find(":checkbox")[0];
$(id).attr('checked',true);
$(id).val( $(cb).val());
$('.li-autoList').remove();
// autoList(id,list);
});
$(id).bind({
'keyup': function() {
$('.itm').removeAttr('checked');
autoSort(id,list)
},
blur: function() {
//if checkbox checked
if(!$('.itm').attr('checked'&&( $(id).val()==''))){
$(id).val(null);
autoList(id,list)
}
}
});
}
var ci={"response":{"status":"0", "data":[{"cod":"SLST" ,"description":"San Luis Talpa" , "group":"SV"},{"cod":"SA6" ,"description":"San Salvador" , "group":"SV"}]}};
var co={"response":{"status":"0", "data":[{"cod":"FR-AG" ,"description":"Agay" , "group":"Altro"},{"cod":"FR-AI" ,"description":"Aix En Provence" , "group":"Altro"},{"cod":"AL" ,"description":"Albania" , "group":"Le più cliccate"},{"cod":"DZ" ,"description":"Algeria" , "group":"Altro"},{"cod":"FR-AM" ,"description":"Amiens" , "group":"Altro"},{"cod":"AD" ,"description":"Andorra" , "group":"Altro"},{"cod":"ZW" ,"description":"Zimbabwe" , "group":"Altro"}]}};
</script>
</body>
回答by lee tan
I think you should be wrong in this line:
我认为您在这一行中应该是错误的:
if (!$('.itm').attr('checked' && ($(id).val()==''))) {
It might look like this:
它可能看起来像这样:
if (!$('.itm').attr('checked') && ($(id).val()=='')) {