您的位置:首页 > Web前端 > JQuery

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

2015-11-06 14:08 525 查看
因项目需要,直接 以$(文本框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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery 扩展 js