2012-10-11 44 views
4

下面的代碼使用this.value來獲取表單下拉列表的值。我只使用過.val()。下面的方式是可以接受的跨瀏覽器(尤其是IE的老版本)?謝謝!Jquery val()vs this.value for dropdowns

$(':input', '#all').each(function() { 
     alert(this.value);     
    }); 

回答

5

是的,這是可以接受的,是更具可讀性,並且比調用$(this).val()更便宜(快)。

簡而言之,$(this)引用了jQuery對象,而this引用了DOM元素。

here觸及它的常見問題簡要(下「瞭解你的DOM屬性和功能」)

應使用普通「這種」原生的DOM API足矣時,和 $(本)當您需要jQuery的幫助。

我還建議您閱讀以下內容:

$(this) vs this in jQuery

jQuery: What's the difference between '$(this)' and 'this'?

When to use Vanilla JavaScript vs. jQuery?

utilizing-the-awesome-power-of-jquery-to-access-properties-of-an-element

this demystified

+0

謝謝!好的答案 – danielb

+0

你需要很多的輸入字段,直到你會注意到性能的差異 –

-1

這應該工作

的jQuery val你需要$(this).val()