1 回答

TA贡献2003条经验 获得超2个赞
下拉选择和取值思路
以下为我的思路
每个行实例都具有一个
value
对象,如:this.value = {}
;行内的
select
使用标准的数字值,用以比对数组下标,确保取值的准确;每个
select
的change
事件都会修改一个对应的值,如:第一个select.type
选中时,在联动代码之前添加this.value.type = $type[0].value
—— 自行代入对应的变量;使用隐藏域来管理输出,如:
<input type="hidden" name="name[]" />
;
你可以使用任意的数据格式,个人建议尽量使用数字,如:
// 数组
this.value = [1,1,1,0]; // 预缴-月度|2016年|1月|查账征收
// 对象
this.value = {
type: 2, // 预缴-季度
years: 1, // 2016年
time: 3, // 第四季度
fangs:1 // 核定征收
};
最后的 select.fangs
选择时会生成可用数据,以供表单使用,如:
$fangs.on('change.app', $.proxy(function(e){
// 添加值
this.value.fangs = $fangs[0].value;
// 生成表单值
this.$output.val(JSON.stringify(this.value));
}, this));
关于验证
扩展一个 验证方法,如:AppRow.prototype.validate()
,该方法根据 this.value
的内容监测表单的合法性,并返回一个布尔值,如:
首先是否为空对象,是,则说明本行第一个
select
为请选择
,返回false
;如果选择了
.type - 0 - 年度汇算
(第一个select
),则获取AppRow.typeChose[this.value.type]
对象;对象不存在,说明可能是越界,如根本不存在
AppRow.typeChose[4]
, 返回false
;根据所选
AppRow.typeChose[this.value.type].types
得出typeSubLen
当前类型对应的后面表单的数量,即当前类型应该有几个相关的子级;如果
(this.value.length - 1) < typeSubLen
说明长度不足,缺少参数,又或!this.value.time1
不存在,返回false
;this.value.time1
存在,值=== -1
或!AppRow.typeChose[0].types[this.value.time1]
,返回false
;this.value.time2
存在,值=== -1
或!AppRow.typeChose[0].types[0].zType_time1[this.value.time1]
,返回false
;!this.value.fangs
或this.value.fangs === -1
,返回false
;条件都满足,返回
true
;
添加回答
举报