对jquery val 获取input 文本框值进行扩展

来源:互联网 发布:方可进销存软件下载 编辑:程序博客网 时间:2024/06/05 06:03

因项目需要,直接 以$(文本框name名称).value() 形式获取 或者 设置 其值,原jquery 自带不是很能满足需要,现在 进行扩展插件

fox.风来了

;(function($,window,document,undefined){        $.fn.value = function(options) {                var _selector=this.selector,$this=$(_selector),val;                if($this.length<=0){                        var first = _selector.substr(0,1);                        if("#" === first || "." === first){                                $this = $(_selector);                        } else {                                $this = $("[name='" + _selector + "']");                        }                }                if(options===undefined){                        if($this.eq(0).is(":radio")) { //单选按钮                                val =$this.filter(":checked").val();                        } else if($this.eq(0).is(":checkbox")) { //复选框                                val='';                                $this.filter(":checked").each(function(i){                                        val+=(i==0?'':',')+$(this).val()                                });                        } else {                                val = $this.val();                        }                        //判断是否是数值文本框                        if($this.attr('type')=='number'){                                if(isNaN(val)){                                        val=0;                                }else if(val==''){                                        val=0;                                }                        }                }else{                        //判断是否是数值文本框                        if($this.eq(0).is(":radio")) {                                $this.filter("[value='" + options + "']").each(function () {                                        this.checked = true                                });                                return true;                        }else if($this.eq(0).is(":checkbox")){                                if(!$.isArray(options)&&options&&options.indexOf(',')>0){                                        $this.val(options.split(','));                                }                                return true;                        }else{                                $this.val(options);                        }                        return true;                }                return val;        }})(jQuery,window,document);

使用方法
1.获取值
   $('test').value()
   页面中有 test 文本框时就获取该值

2.设置值
$('test').value('内容')


支持,type=text,radio,checkbox,textarea,select

0 0
原创粉丝点击