3

如果我有这样的对象:

var item = {
    id: '1',
    description: 'something'
}

那么我如何提取这些值之一呢?如果我做

alert($(item.id));

然后我只是收到一个警告说'对象对象'。我看了一下这篇文章:从 jQuery 对象获取基本元素并尝试调用

alert($(item).get(0)); 

但结果相同。

另外,从术语的角度来看,这样的对象叫什么,以便下次我可以更具体一点?

4

4 回答 4

5

只需使用:

alert(item.id);

这:$(item.id)将 item.id 包装到一个 jQuery 对象中,这就是你得到“对象对象”的原因

附言

您正在创建的是对象文字表示法中的对象,即花括号内的一组逗号分隔的名称/值对。

于 2012-04-24T08:42:57.917 回答
0
console.log(item.id)

$()返回一个 jQuery 对象。

于 2012-04-24T08:48:31.313 回答
0

试试这个:

警报($item .prop('id'));

于 2012-04-24T08:48:33.187 回答
0

使用正常程序:

alert(item.id);

使用 jQuery:

alert($(item).get(0).id);

笔记

 $(item) -> return an array

 $(item).get(0) -> return object which is equal to (var item)

 $(item).get(0).id -> return id value
于 2012-04-24T08:53:52.203 回答