2011-08-05 99 views
-1

我需要在jquery中查詢選擇值爲「」的元素。我在IE中工作,但沒有在Chrome中。我認爲chrome有另一個價值作爲非定義價值。在鉻中查找具有未定義值的元素

我的查詢:

var images = $('#images>div>input[type=file][value=""]'); 
if(images.length > 0) // ... 

正如你可以看到有這給用戶一個機會,附加圖像文件類型的輸入元素。

<div id="files"> 
<div><input type="file" name="files" /><input type="button" value="Remove" onclick="removeImage(this)" /></div> 
<div><input type="file" name="files" /><input type="button" value="Remove" onclick="removeImage(this)" /></div> 
<div><input type="file" name="files" /><input type="button" value="Remove" onclick="removeImage(this)" /></div> 
</div> 

在IE中,它工作的很好,但在鉻它總是返回0個元素。

你有任何解決方案可以在每個瀏覽器上工作嗎?

+0

你可以發佈標記(HTML)嗎? (另外,你嘗試過哪些版本的IE?) – Pointy

回答

0

我合併你的兩個答案,最後我寫了這個:

var images = $(':input[type="file"]').filter(function() { 
    return $(this).val()==this.defaultValue; 
}); 

這個偉大的工程。謝謝。

+0

不錯的工作。您可以隨時使用[':file'](http://api.jquery.com/file-selector/)「交換':input [type =」file「]':) – alex

0

嘗試......

var images = $('#images > div > :file') 
       .filter(function() { return $(this).val().length; }); 

的Chrome,火狐& IE肯定能得到value(雖然Chrome瀏覽器只會告訴你C:\fakepath\預謀的文件名)。

What does your browser say?

+0

你的解決方案也沒有工作。我把HTML代碼。 – nosbor

+0

+1 - 這對我幫助 – nosbor

1

這工作:

$('#show').click(function(){ 
    $(':input[type="file"]').each(function(){ 
     if($(this).val() == this.defaultValue) 
     { 
      $(this).css('border','2px solid red'); 
     } 
    }); 
}); 

而不是試圖內容具有可變空字符串或「fakepath」等,只需用自己的defaultValue比較一下,你就會知道比較如果它已被填充或沒有。

http://jsfiddle.net/AlienWebguy/5sK7T/

+0

+1漂亮的傢伙! - – Maverick

+0

+1 - 這幫了我 – nosbor

相關問題