【问题标题】:How do I get a specific value from a jQuery object?如何从 jQuery 对象中获取特定值?
【发布时间】:2012-04-24 08:40:27
【问题描述】:

如果我有这样的对象:

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

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

alert($(item.id));

然后我只收到一条警告说“对象对象”。我看了这篇帖子:Getting the base element from a jQuery object 并尝试调用

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

但结果相同。

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

【问题讨论】:

  • 为什么不只使用alert(item.id);
  • 什么...?为什么你不能只用它的名字和键来调用它..? jQuery 不是javascript之王,你不应该每次都需要你的国王..
  • 真正想做什么?
  • 不幸的是,我是那些在 javascript 之前就开始学习 jQuery 的人之一。
  • @NiftyDude,并不是每个人都是 JavaScript 专家或知道 jQuery 和 JS 之间的区别。我认为你的评论本来可以更好一点

标签: jquery


【解决方案1】:

只需使用:

alert(item.id);

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

PS

您正在创建的是object literal notation 中的一个对象,它是花括号内的一组逗号分隔的名称/值对。

【讨论】:

    【解决方案2】:
    console.log(item.id)
    

    $() 返回一个 jQuery 对象。

    【讨论】:

      【解决方案3】:

      试试这个:

      alert($item .prop('id'));

      【讨论】:

        【解决方案4】:

        使用正常程序:

        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
        

        【讨论】:

          猜你喜欢
          • 2010-11-08
          • 1970-01-01
          • 1970-01-01
          • 2012-07-20
          • 1970-01-01
          • 1970-01-01
          • 2017-11-22
          • 2022-01-21
          • 2019-12-17
          相关资源
          最近更新 更多