2011-04-19 53 views
0

我有此腳本:如何檢查無功在jQuery的

var display = '<div class="uploadData" id="pxupload'+ itr +'_text">'; 

,我想檢查是否從diplaydiv存在。類似這樣的:

if (display = null){ 
     $("#px_display").append(display); 
    }else if (display != null) { 
     $(config.buttonClear).trigger('click'); 
    } 

我不知道該陳述是否正確。

回答

0

你可以做的最好的是...

var display = '<div class="uploadData" id="pxupload'+ itr +'_text">'; 

var id = $(display).attr('id'); 

if ($('#' + id).length) { 
    // Some element with the same id attribute exists. 
} 

這,當然,是不是太準確。

這將是更好,如果你第一次做這個變量,像這樣......

var displayId = 'pxupload' + itr; 

if ($('#' + displayId).length) { 
    // Some element with the same id attribute exists. 
} 
1

由於ID將是唯一的和構造的,你可以這樣做:

if($("#pxupload" + itr + "_text") == null){ 
     $("#px_display").append(display); 
}else if (display != null) { 
    $(config.buttonClear).trigger('click'); 
}