jQuery的.attr()和值 [英] jQuery .attr() and value
问题描述
我想作以下用途.attr();
selectbox.options [selectbox.selectedIndex] .value的
不幸的是,
<$ P为$ P> selectbox.options [selectbox.selectedIndex] .attr(值)
是不一样的,而且似乎完全打败.attr的目的。我的主要问题是:一个人应该如何使用嵌套.attr()■
Muchos格拉西亚斯英里吾友
要得到任何类型的输入元素的值(包括&LT; textarea的&GT;
和&LT;选择&GT;
)使用 .VAL()
:
VAR值= $(选择框).VAL();
.attr()
翻译将大致是:
$(选择框).find(:选择)。ATTR(值);
....只是使用 .VAL()
:)
最根本的问题是, .attr()
是一个jQuery的方法。这是jQuery的对象,而不是DOM元素直接,也是如此近的所有的jQuery方法和插件。
I want to make the following use .attr();
selectbox.options[selectbox.selectedIndex].value
sadly,
selectbox.options[selectbox.selectedIndex].attr("value")
is not the same, and seems to defeat the purpose of the .attr altogether. My main question is: How should one use nested .attr()s?
Muchos gracias mi amigos
To get the value of any type of input element (including <textarea>
and <select>
) use .val()
:
var value = $(selectbox).val();
The .attr()
translation would roughly be:
$(selectBox).find(":selected").attr("value");
....but just use .val()
:)
The basic problem is that .attr()
is a jQuery method. It's on jQuery objects, not on DOM elements directly, the same goes for almost all jQuery methods and plugins.
这篇关于jQuery的.attr()和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!