jQuery val()与this.value的下拉列表 [英] Jquery val() vs this.value for dropdowns
问题描述
以下代码使用this.value获取表单下拉列表的值.我只大致看到使用.val().以下方式是否可以接受跨浏览器(尤其是IE的旧版本)?谢谢!
The below code uses this.value to get the value of a forms dropdowns. I have only generally seen .val() used. Is the below way acceptable cross-browser (especially older verions of IE)? Thanks!
$(':input', '#all').each(function() {
alert(this.value);
});
推荐答案
是的,可以接受,比调用 $(this).val()
更易读,并且更便宜(更快).
Yes, it's acceptable, is more readable, and is less expensive (faster) than calling $(this).val()
.
简单地说, $(this)
是指jQuery对象,而 this
是指DOM元素.
Simply put, $(this)
refers to a jQuery object, whilst this
refers to a DOM element.
常见问题解答此处对此进行了简要介绍(在了解您的DOM属性和功能"下)
The FAQ here touches upon it briefly (under 'Know Your DOM Properties and Functions')
当本地DOM API足够时,您应该使用普通的"this",并且$(this)当您需要jQuery的帮助时.
You should use plain "this" when the native DOM APIs suffice, and $(this) when you need the help of jQuery.
我还建议您阅读以下内容:
I'd also suggest reading the following:
何时使用Vanilla JavaScript与jQuery?
这篇关于jQuery val()与this.value的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!