Javascript != 不比较字符串
我有以下代码,当我运行代码时,警报显示>未定义<,但if条件无法比较字符串 我将Javascript != 不比较字符串,javascript,jquery,Javascript,Jquery,我有以下代码,当我运行代码时,警报显示>未定义:不是(.ui自动完成类别)”; }, _renderMenu:功能(ul,项目){ var=this,currentCategory=“”; $。每个(项目、功能(索引、项目){ 李华; 如果(item.category!=currentCategory){ 如果(item.category!=“undefined”){//,则它不起作用! 警报(“>”+item.category+”如果您想将未定义的字符串与使用typeof获得的字符串进行比较,
item.category
放在一个新的var
中,但没有帮助
$.widget("custom.catcomplete", $.ui.autocomplete, {
_create : function() {
this._super();
this.widget().menu("option", "items",
"> :not(.ui-autocomplete-category)");
},
_renderMenu : function(ul, items) {
var that = this, currentCategory = "";
$.each(items, function(index, item) {
var li;
if (item.category != currentCategory) {
if(item.category != "undefined"){//it does not work!
alert(">" + item.category + "<"); //returns >undefined<
ul.append("<li class='ui-autocomplete-category'><b>"
+ item.category + "</b></li>");
}else{
alert("not");
ul.append("<li class='ui-autocomplete-category'></li>");
}
currentCategory = item.category;
}
li = that._renderItemData(ul, item);
if (item.category) {
li.attr("aria-label", item.category + " : "
+ item.label);
}
});
}
});
$.widget(“custom.catcomplete”,$.ui.autocomplete{
_创建:函数(){
这个;
此.widget()菜单(“选项”、“项目”,
“>:不是(.ui自动完成类别)”;
},
_renderMenu:功能(ul,项目){
var=this,currentCategory=“”;
$。每个(项目、功能(索引、项目){
李华;
如果(item.category!=currentCategory){
如果(item.category!=“undefined”){//,则它不起作用!
警报(“>”+item.category+”如果您想将未定义的字符串与使用typeof获得的字符串进行比较,我将给您一个示例
if(typeof variable === 'undefined')
mate,undefined是类型而不是字符串
item.category != "undefined"
致:
或:
项。类别
只是未定义
。未定义
值与“未定义”
字符串不同
undefined == 'undefined'; //false
但是,将undefined
转换为字符串将产生“undefined”
。在字符串和任何其他数据类型之间使用+
运算符时,其他数据类型将首先转换为字符串
alert(“>”+item.category+“+String(item.category)+”在我看来,item.category
属性是未定义的(请参阅),而不是定义为值为“未定义”
的字符串
与未定义的不同
@epascarello undefined!=(一个空字符串)投反对票的原因是什么?我不是猜测的天才:D
if(item.category)
{
//code
}
undefined == 'undefined'; //false
if (typeof item.category == 'undefined') { //undefined }
if (item.catgegory == null) { //null or undefined }