jQuery的.attr()和值 [英] jQuery .attr() and value

查看:128
本文介绍了jQuery的.attr()和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作以下用途.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆