jquery val 扩展

故事发生的背景,原来的模块已经各地引用了

下面是图片上传成功之后的通用过程

parentObj.siblings(".upload-name").val(data.name);
parentObj.siblings(".upload-path").val(data.path);
parentObj.siblings(".upload-size").val(data.size);

 

通过扩展$.val给img赋值

//$.prototype.val = function (base) {
//                return function () {
//                    var s = this;
//                    var a = arguments;
//                    s.each(function (i, j) {
//                        if ($(j).is('img')) {
//                            if (a && a.length > 0) {
//                                $(j).attr('src', a[0]);
//                            }
//                        }
//                    });
//                    base.call(s, arguments[0]);
//                }
//            }($.prototype.val);

 20151109更新,做到后面发现有问题

$.prototype.val = function (base) {
                return function () {
                    var s = this;
                    var a = arguments;
                    s.each(function (i, j) {
                        if ($(j).is('img')) {
                            if (a && a.length > 0) {
                                $(j).attr('src', a[0]);
                            }
                        }
                    });
                    if (arguments.length > 0) {
                        return base.call(s, arguments[0]);
                    } else {
                        return base.call(s);
                    }
                }
            }($.prototype.val);

 

posted @ 2015-11-07 13:59  卖果子的小男孩  阅读(291)  评论(0编辑  收藏  举报