jQuery .each对象 [英] jquery .each objects

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

问题描述

我正在构建一个jQuery插件,但是我想在var选项中使用对象,我该如何使用jQuery循环这些对象?

i am building an jQuery plugin, but i want to use objects in the options var, how can i loop this with the jQuery each?

插件选项var

        var defaults = {
            test: 'yes',   //css/classes
            type: {
                minvalue: '100',
                maxvalue: '200',
                name: 'id1'
            },
            type: {
                minvalue: '200',
                maxvalue: '300',
                name: 'id2'
            },
            type: {
                minvalue: '300',
                maxvalue: '400',
                name: 'id3'
            }               
        };


            $.each(defaults, function(key, value) { 
              alert(key + ': ' + value); 
            });  

推荐答案

以您的示例为例,很难弄清您要尝试执行的操作,但是如果要使用arrays$.each,您会发现可以做到这一点:

With your example it is a bit difficult to figure what exactly you are trying to do, but if you want to use arrays and $.each, you could do this:

 var defaults = {
            test: 'yes',   //css/classes
     types: [
         {
                minvalue: '100',
                maxvalue: '200',
                name: 'id1'
            },
         {
                minvalue: '200',
                maxvalue: '300',
                name: 'id2'
            },
         {
                minvalue: '300',
                maxvalue: '400',
                name: 'id3'
            }


         ]

        };

$.each(defaults.types, function(index, value) { 
              alert(value.name + ': ' + value.minvalue); 
            }); 

http://jsfiddle.net/niklasvh/kFjVN/

这篇关于jQuery .each对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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