1

下面的代码添加了一个上传字段的链接。对于每一次新点击,它都会在表单中显示一个新的上传字段,最多 5 个。Chrome 等中没有错误。

我想知道下面这行有什么问题,因为该脚本似乎在其他浏览器上运行良好,但在 IE8 上它会引发错误:Object doesn't support this action. 你能建议替代代码吗?

<div id="edit-submitted-file1-ajax-wrapper" style="display: block;">
    //upload field here
</div>
<a id="addmore" href="#">[+] Add more</a>

<div id="edit-submitted-file2-ajax-wrapper" style="display: block;">
    //upload field here
</div>

<div id="edit-submitted-file3-ajax-wrapper" style="display: block;">
    //upload field here
</div>

ETC

first = $('.webform-client-form').find('div[id$="-ajax-wrapper"]').first();
        first.after('<a id="addmore" href=#>[+] Add more</a>');
        $('.webform-client-form').find('div[id$="-ajax-wrapper"]').each(function(){
            $(this).hide();
            first.show();
        });

var c = 0;
$('#addmore').bind('click', function(e) {
    //HERE BELOW IS THE LINE WITH ERROR
    item = $('#edit-submitted-file'+ c +'-ajax-wrapper');
    item.show();
    ++c;
    if (c == 5) {
        $('#addmore').hide();
        return false;
    }
});
4

2 回答 2

1

改变这个

item = $('#edit-submitted-file'+ c +'-ajax-wrapper');

对此

var item = $('#edit-submitted-file'+ c +'-ajax-wrapper');

http://jsfiddle.net/fJ3EG/2/

于 2012-12-19T10:06:03.173 回答
0

您之前可能在 html 中的某处使用过“项目”一词。将您的变量声明为var item,问题应该得到解决。

请参阅 Stackoverflow 上的早期帖子:

我的猜测是,您的页面上有一些名称或 id 为“item”的东西,因此这已成为 window 的属性,因为 IE 会这样做(...)

即,javascript和'item'作为变量名

于 2013-01-20T13:50:10.550 回答