1

使用这个(jquery for fileupload)脚本我收到一些错误,但它在本地的 wamp 中工作。对于生产,我需要停止此警报错误”

“SyntaxError:属性列表
progressall之后缺少}:函数(e,数据){”

或在 Chrome 中:

“第 211 行未捕获的语法错误意外标识符”

与 Firefox 中的行相同。

有人有想法吗?

 $(function () {
     $('#fileupload').fileupload({
        dataType: 'json',       
        done: function (e, data) {
        $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);
        });
    }
    progressall: function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    $('#progress .bar').css(
        'width',
        progress + '%'
     );
    }
    add: function (e, data) {
        data.context = $('<p/>').text('Uploading...').appendTo(document.body);
        data.submit();
    }
    done: function (e, data) {
        data.context.text('Upload finished.');
    }
    add: function (e, data) {
        data.context = $('<button/>').text('Upload')
            .appendTo(document.body)
            .click(function () {
                $(this).replaceWith($('<p/>').text('Uploading...'));
                data.submit();
            });
    }
    done: function (e, data) {
        data.context.text('Upload finished.');
    }

     });
});

我做了一些修改:Mozilla 没有错误但无法正常工作

在 chrome 错误中(Uncaught TypeError: Cannot call method 'push' of undefined)并且不工作

$(function () {
    //declare a "updloadOptions" variable object that will be passed to the plugin constructor method as a parameter. (You can give any name to this object.)
    var updloadOptions = {};

    //set the datatype property to 'json'.
    updloadOptions.dataType = 'json';
    //declare the "done" callback method on "updloadOptions" object.
    updloadOptions.done = function (e, data) {
        $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);
        });
    };
    //declare the "progressall" callback method on "updloadOptions" object.
    updloadOptions.progressall = function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
        progress + '%');
    };
    //declare the "add" callback method on "updloadOptions" object.
    updloadOptions.add = function (e, data) {
         data.context = $('<button/>').text('Upload')
         .appendTo(document.body)
         .click(function () {             $(this).replaceWith($('<p/>').text('Uploading...'));                
        data.context = $('<p/>').text('Uploading...').appendTo(document.body);
        data.submit();
        });
    };
    //initialize the component
    $('#fileupload').fileupload(updloadOptions);
});

语法错误的正确脚本

SyntaxError: 在属性列表
filesContainer: $('.filescontainer')之后缺少 }

而且我永远不需要filesContainer,因为我用上传系统检索了第二个 jquery 选项卡

$(function () {
$('#fileupload').fileupload({
   dataType: 'json',       
   done: function (e, data) {
       $.each(data.result.files, function (index, file) {
           $('<p/>').text(file.name).appendTo(document.body);
       });
   },
   progressall: function (e, data) {
   var progress = parseInt(data.loaded / data.total * 100, 10);
   $('#progress .bar').css(
       'width',
       progress + '%'
    );
   },
   add: function (e, data) {
       data.context = $('<p/>').text('Uploading...').appendTo(document.body);
       data.submit();
   },
   done: function (e, data) {
       data.context.text('Upload finished.')
   },
   add: function (e, data) {
       data.context = $('<button/>').text('Upload')
           .appendTo(document.body)
           .click(function () {
               $(this).replaceWith($('<p/>').text('Uploading...'));
               data.submit();
           });
   },  done: function (e, data) {
       data.context.text('Upload finished.')
   }
   filesContainer: $('.filescontainer') 

});
});
4

1 回答 1

2

您的脚本存在会产生多个错误的基本错误。

  • 您应该在传递给.fileupload()插件的选项对象的每个成员的末尾都有逗号。
  • 您已声明重复的回调方法。done回调被声明了 3 次,add回调被声明了两次。

因此,您应该只使用每个重复减速中的一个,或者将这些重复中的代码合并为一个。但我看到这些回调中的代码也是重复的。

这是您的代码的清理版本:

<script type="text/javascript">
    $(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            done: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo(document.body);
                });
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .bar').css('width', progress + '%');
            },
            add: function (e, data) {
                data.context = $('<p/>').text('Uploading...').appendTo(document.body);
                data.submit();
            }
        });
    });
</script>

这是一个更易读的版本:

<script type="text/javascript">

    $(function () {

        //declare a "updloadOptions" variable object that will be passed to the plugin constructor method as a parameter. (You can give any name to this object.)
        var updloadOptions = {};

        //set the datatype property to 'json'.
        updloadOptions.dataType = 'json';

        //declare the "done" callback method on "updloadOptions" object.
        updloadOptions.done = function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        };

        //declare the "progressall" callback method on "updloadOptions" object.
        updloadOptions.progressall = function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css('width', progress + '%');
        };

        //declare the "add" callback method on "updloadOptions" object.
        updloadOptions.add = function (e, data) {
            data.context = $('<p/>').text('Uploading...').appendTo(document.body);
            data.submit();
        };

        //initialize the component
        $('#fileupload').fileupload(updloadOptions);

    });

</script>
于 2013-01-21T13:42:10.023 回答