【问题标题】:JQuery Ajax Plugin File Upload simplify codeJQuery Ajax 插件文件上传简化代码
【发布时间】:2010-02-24 07:30:42
【问题描述】:

我目前正在使用 Valums JQuery 文件上传插件。该插件使用起来非常方便,但我不喜欢代码的外观。因为它占用了document.ready里面如:

$(document).ready(function() {
    var button = $('#button1'), interval;

    new AjaxUpload(button, {
        action: 'http://test.com/user/uploadfile', 
        name: 'myfile',
        onSubmit : function(file, ext){
            // change button text, when user selects file           
            button.text('Uploading');

            // If you want to allow uploading only 1 file at time,
            // you can disable upload button
            this.disable();

            // Uploding -> Uploading. -> Uploading...
            interval = window.setInterval(function(){
                var text = button.text();
                if (text.length < 13){
                    button.text(text + '.');                    
                } else {
                    button.text('Uploading');               
                }
            }, 200);
        },
        onComplete: function(file, response){
            button.text('Upload Finished');

            window.clearInterval(interval);

            // enable upload button
            //this.enable();

            alert(response);            
        }
    });
});

我的问题是代码可以更简化吗?这样我就可以或多或少地让代码看起来像:

$(document).ready(function() {
    var button = $('#button1'), interval;

    new AjaxUpload(button, {#leads to another function}#);
});

提前致谢

【问题讨论】:

    标签: jquery file jquery-plugins upload


    【解决方案1】:

    您是 Javascript 新手吗?我记得我第一次使用 Javascript 和 jQuery 的经历很有趣,因为我不喜欢我的代码最初的样子。问题是你真的必须习惯它并理解它的存在是有原因的。编写一个自定义插件来做这样的事情是可行的,但最后,你仍然会把代码藏在某个地方。只需选择您喜欢的代码嵌套方案,并为您的工作感到自豪,因为 99% 的客人不会真正关心 =)

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多