当前位置 : 主页 > 网络编程 > JavaScript >

这是一个专门用来进行文件选择的控件

来源:互联网 收集:自由互联 发布时间:2021-06-28
selectfilecontrol.js /** 这是一个专门用来进行文件选择的控件**//// (function ($, window) { function selectfile(dom, setting) { this.setting = $.extend(true, {}, selectfile.setting, setting); this.dom = $(dom); this.selectfil
selectfilecontrol.js
/*
* 这是一个专门用来进行文件选择的控件*
*/
/// 

(function ($, window) {
    function selectfile(dom, setting) {
        this.setting = $.extend(true, {}, selectfile.setting, setting);
        this.dom = $(dom);
        this.selectfiles = null;
    }
    selectfile.prototype.initstruct = function () {
        this.listeninputfile = this.dom.find('input[type="file"]');
        this.filenameshowblock = this.dom.find(".filename");
    }
    selectfile.prototype.bindevent = function () {
        var _self = this;
        this.listeninputfile.change(function () {
            _self.selectfiles = this.files;
            var names = "";
            for (var i = 0; i < this.files.length; i++) {
                names += this.files[i].name + ";";
            }
            names = names.substr(0, names.length - 1);
            if (_self.filenameshowblock.is("input")) {
                _self.filenameshowblock.val(names);
            } else {
                _self.filenameshowblock.html(names);
            }
        })
    }
    selectfile.setting = {
    }

    $.fn.selectfile = function (parameters) {
        var $allModules = $(this);
        var query = arguments[0];
        var methodInvoked = (typeof query == 'string');
        var queryArguments = [].slice.call(arguments, 1);
        var returnedValue;
        $allModules.each(function () {
            if (methodInvoked) {
                var sf = $(this).data("selectfile");
                if (sf) {
                    returnedValue = sf[query].apply(sf, queryArguments);
                }
            } else {
                var sf = $(this).data("selectfile");
                if (sf) {
                    sf.destory();
                }
                $(this).data("selectfile", new selectfile(this, parameters));
            }
        });
        return (returnedValue !== undefined) ? returnedValue : this;
    }
}(jQuery, window))
网友评论