通用表单组件取值赋值
说明
easyui
有多个表单组件,textbox
, validatebox
, combobox
, combogrid
, combotree
, numberbox
, numberspinner
。组件设置值和获取值需要的方各不相同,在实际使用中很不方便,此处提供几个方法,用来通用的获取和设置表单组件值。 共有:getText
getVal
setVal
getTV
easyVal
getText()
- 获取输入框文本,普通文本框text 和value一致,combo text为输入框值,value为真实返回值
$('.btn-getTxt').click(function (){
var a = $('#txt-normal').getText();
var b = $('#txt-textbox').getText();
var c = $('#txt-validate').getText();
var d = $('#txt-combobox').getText();
var e = $('#txt-combogrid').getText();
var f = $('#txt-combotree').getText();
var g = $('#txt-num').getText();
var h = $('#txt-numsp').getText();
var i = $('#txt-date').getText();
console.log('normal',a);//normal 文本1
console.log('textbox',b);//textbox 文本2
console.log('validate',c);//validate 文本3
console.log('combobox',d);//combobox 注射剂
console.log('combogrid',e);//combogrid 益视颗粒,马应龙八宝眼膏
console.log('combotree',f);//combotree 营销中心查询,门诊医生
console.log('num',g);//num 888
console.log('numsp',h);//numsp 76
console.log('date',i);//date 2020-12-14
});
getVal(returnArr)
- 获取输入框值,普通文本框text 和value一致,combo text为输入框值,value为真实返回值
returnArr
为 true,combo组件以数组形式返回值
$('.btn-getVal').click(function (){
var ra = $('.chk-returnArr').prop('checked');//是否返回数组,对combo组件有效,为true返回数组
var a = $('#txt-normal').getVal();
var b = $('#txt-textbox').getVal();
var c = $('#txt-validate').getVal();
var d = $('#txt-combobox').getVal(ra);
var e = $('#txt-combogrid').getVal(ra);
var f = $('#txt-combotree').getVal(ra);
var g = $('#txt-num').getVal();
var h = $('#txt-numsp').getVal();
var i = $('#txt-date').getVal();
console.log('normal',a);//normal 文本1
console.log('textbox',b);//textbox 文本2
console.log('validate',c);//validate 文本3
console.log('combobox',d);//combobox 2 || ["2"]
console.log('combogrid',e);//combogrid 46yP || ["46yP"]
console.log('combotree',f);//combotree 1111,1113 || ["1111", "1113"]
console.log('num',g);//num 888
console.log('numsp',h);//numsp 76
console.log('date',i);//date 2020-12-14
});
setVal(val)
- val: Number | String | Object | Array
- 为数字、字符串、对象为单选赋值,数组 为多选赋值,主要用在combo组件中
$('.btn-setVal').click(function (){
$('#txt-normal').setVal('文本1');
$('#txt-textbox').setVal('文本2');
$('#txt-validate').setVal('文本3');
$('#txt-combobox').setVal('2');
$('#txt-combogrid').setVal([{//多选赋值
id: "46yP",
item_code: "2hBT",
item_name: "益视颗粒"
},{
id: "rrBB",
item_code: "T6Cp",
item_name: "马应龙八宝眼膏"
}]);
$('#txt-combotree').setVal(["1111", "1113"]);
$('#txt-num').setVal(888);
$('#txt-numsp').setVal(76);
$('#txt-date').setVal('2020-12-14');
});
getTV(returnArr)
- 获取组件text和value值,返回:
{text:,value:}
,多选值用逗号隔开 returnArr
为true
时,返回数组- 普通输入框返回输入框值
$('.btn-getTV').click(function (){
var ra = $('.chk-returnArr').prop('checked');//是否返回数组,对combo组件有效,为true返回数组
var a = $('#txt-normal').getTV();
var b = $('#txt-textbox').getTV();
var c = $('#txt-validate').getTV();
var d = $('#txt-combobox').getTV(ra);
var e = $('#txt-combogrid').getTV(ra);
var f = $('#txt-combotree').getTV(ra);
var g = $('#txt-num').getTV();
var h = $('#txt-numsp').getTV();
var i = $('#txt-date').getTV();
console.log('normal',a);//normal 文本1
console.log('textbox',b);//textbox 文本2
console.log('validate',c);//validate 文本3
console.log('combobox',d);//combobox {text: "注射剂", value: "2"} || [{text: "注射剂", value: "2"}]
console.log('combogrid',e);//combogrid {text: "益视颗粒,马应龙八宝眼膏", value: "46yP,rrBB"} || [{text: "益视颗粒", value: "46yP"},{text: "益视颗粒", value: "46yP"}]
console.log('combotree',f);//combotree {text: "营销中心查询,门诊医生", value: "1111,1113"} || [{text: "营销中心查询", value: "1111"},{text: "门诊医生", value: "1113"}]
console.log('num',g);//num 888
console.log('numsp',h);//numsp 76
console.log('date',i);//date 2020-12-14
});
easyVal(val)
setVal
和getVal
综合方法,v存在为set,否则为get
$('.btn-easyVal').click(function (){
var a = $('#txt-normal').easyVal();
var b = $('#txt-textbox').easyVal();
var c = $('#txt-validate').easyVal();
var d = $('#txt-combobox').easyVal(2);
var e = $('#txt-combogrid').easyVal('46yP');
var f = $('#txt-combotree').easyVal(["1111", "1113"]);
var g = $('#txt-num').easyVal();
var h = $('#txt-numsp').easyVal();
var i = $('#txt-date').easyVal();
console.log('normal',a);//normal 文本1
console.log('textbox',b);//textbox 文本2
console.log('validate',c);//validate 文本3
// console.log('combobox',d);
// console.log('combogrid',e);
// console.log('combotree',f);
console.log('num',g);//num 888
console.log('numsp',h);//numsp 76
console.log('date',i);//date 2020-12-14
});