设置'动态'变量时遇到问题

时间:2013-02-19 20:52:51

标签: jquery variables

我想动态设置变量名称,我一直在尝试:

var iframes = $('iframe').filter(function (index) {
                return index == 0 || $(this).attr("current") == "no";
              })
        if(iframes.length >0){ //if one of the iframes hasnt got current set as current, use it
            var theSuffix = iframes.attr('id').split('_').pop();
            window['thisPreview'+theSuffix] = $fi.prev(".image-preview");
            $hidden.closest(".file-upload-form").find(".variable-hidden").attr('value',theSuffix);
        }

我需要一个名为'thisPreview'的变量,然后是一个数字,它将是匹配div的id,或变量'theSuffix'(变量也需要是全局变量)。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

不要在末尾创建具有公共前缀和数字的变量。只需使用一个对象:

var thisPreview = {};

...

thisPreview[theSuffix] = $fi.prev(".image-preview");
相关问题