json数据渲染表单元素出现的问题

解析页面表单元素

 parseForm: function () {
var data = {};
$(this).find('input').each(function () {
switch ($(this).attr('type')) {
case 'radio':
case 'checkbox':
if ($(this).prop('checked')) {
data[$(this).attr('name')] = $(this).val();
}
break;
default:
data[$(this).attr('name')] = $(this).val();
} });
$(this).find('select').each(function () {
data[$(this).attr('name')] = $(this).val();
});
$(this).find('textarea').each(function () {
data[$(this).attr('name')] = $(this).val();
});
return data;
}
}); json数据渲染表单元素
 loadForm: function (jsonstr) {
try {
var jsondata = jsonstr == '' ? '' : $.parseJSON(jsonstr);
} catch (e) {
throw new Error('非法的json字符串~');
}
var self = this;
var form = {
input: function (name, val) {
$(self).find('input[name="' + name + '"]').val(val);
},
checkbox: function (name, val) {
$(self)
.find('input[name="' + name + '"][type="checkbox"][value="' + val + '"]')
.attr('checked', 'checked');
},
radio: function (name, val) {
$(self)
.find('input[name="' + name + '"][type="radio"][value="' + val + '"]')
.attr('checked', 'checked');
},
textarea: function (name, val) {
$(self).find('textarea[name="' + name + '"]').val(val);
},
select: function (name, val, data) {
var obj = $(self).find('select[name="' + name + '"]');
if ($.isEmptyObject(obj)) {
return false;
}
if (typeof data == 'string') {
obj.append(decodeURIComponent(data.replace(/\+/g, '%20')));
} var selected = obj.find('option[value="' + val + '"]');
if ($.isEmptyObject(selected)) {
obj.find('option:eq(0)').attr('selected', 'selected');
} else {
selected.attr('selected', 'selected');
}
}
};
if (typeof jsondata == 'object') {
for (var key in jsondata) {
var d = jsondata[key];
if (typeof d != 'object' || d.length <= 0) {
continue;
}
if (typeof d.name == 'undefined') {
continue;
}
switch (d.type) {
case 'select':
form.select(d.name, d.value, d.data);
break;
case 'radio':
form.radio(d.name, d.value);
break;
case 'checkbox':
form.checkbox(d.name, d.value);
break;
case 'textarea':
form.textarea(d.name, d.value);
break;
default :
form.input(d.name, d.value);
}
}
}
},
php代码
$formObj = new Form();
$select_arr = ['zulinqixian', 'chanzhengxingzhi', 'pingmiantu', 'gongrefangshi', 'xiaofangpiwen', 'zhengzhaobanli', 'shifouhanpiao', 'dizeng', 'dizengzhi', 'ya', 'fu',
'mianzuqi', 'kongtiaoleixing', 'liangyiqu', 'xiyiqu', 'tingchechangdi', 'dianti',];
foreach (json_decode($ProjectObj->getContent(), true) as $k => $v) {
if ($k == 'yuanxiaofangsheshi-1') {
$formObj->addCheckboxInput('yuanxiaofangsheshi-1', $v);
} else if ($k == 'yuanxiaofangsheshi-2') {
$formObj->addCheckboxInput('yuanxiaofangsheshi-2', $v);
} else {
if(in_array($k,$select_arr)){
$formObj->addSelect($k, $v, []);
}
}
}
   
上一篇:hibernate之自定义持久化实现


下一篇:解决 js setTimeout 传递带参数的函数无效果