jqueryフォーム要素の自動取得と入力
13580 ワード
// , json
/*
* , ,
* easyui , easyui , if , else
* :
* id=" + _auto";
*radio checkbox name=" + _auto";
* _auto id name, data, json
* :_getAutoObject('#form1');
*/
//null undefined
function nullToSpace (v) {
return !v && v != '0' ? '' : v;
}
// eval
function evil (fn) {
var Fn = Function; // Function,
return new Fn('return ' + fn)();
}
function _getAutoObject(obj) {
var data = {};
obj = obj || 'body';
// ,
$(obj).find('*[id$=_auto],*[name$=_auto]').each(function () {
var $t = $(this),
$t_id = $t.attr('id'),
$t_name = $t.attr('name'),
$t_class = $t.attr('class'),
$key;
if ($t.attr('type') != 'radio' && $t.attr('type') != 'checkbox') {
$key = $t_id;
} else {
$key = $t_name;
}
$key = $key.substring(0, $key.length - 5);
// easyui
if ($t_class && ($t_class.indexOf('combo-f') != '-1' || $t_class.indexOf('textbox-f') != '-1' || $t_class.indexOf('numberbox-f') != '-1' || $t_class.indexOf('validatebox-f') != '-1' || $t_class.indexOf('datebox-f') != '-1' || $t_class.indexOf('datetimebox-f') != '-1' || $t_class.indexOf('datetimespinner-f') != '-1' || $t_class.indexOf('numberspinner-f') != '-1')) {
var c = $t_class.split('-f')[0].split(' '),
isVals = '$("'+ obj +' #' + $key + '_auto").' + c + '("options").multiple',
v = 'getValue';
c = c[c.length - 1];
if (c.indexOf('combo') != -1) {
try {
if (evil(isVals)) {
v = 'getValues';
}
} catch (e) {
}
}
var func = '$("'+ obj +' #' + $key + '_auto").' + c + '("' + v + '")';
try {
data[$key] = evil(func);
} catch (e) {
}
} else {
//
if ($t.is('span') || $t.is('div') || $t.is('a') || $t.is('p')) {
data[$key] = $t.text();
} else if ($t.is('img')) {
data[$key] = $t.attr('src');
} else if ($t.is(':input')) {
if ($t.attr('type') == 'checkbox') {
data[$key] = 0;
var c = '*[type="checkbox"][name="' + $key + '_auto"]',
$val;
if ($(c).length > 1) {
$val = [];
$(c).each(function (i) {
if ($(c).eq(i).is(':checked')) {
$val.push($(c).eq(i).val());
}
});
} else {
if ($t.is(':checked')) {
if ($val == '' || $val == null || $val == 'on') {
$val = 1;
}
} else {//
$val = 0
}
}
data[$key] = $val;
} else if ($t.attr('type') == 'radio') {
data[$key] = '';
var c = '*[type="radio"][name="' + $key + '_auto"]:checked';
if ($(c).length > 0) {
data[$key] = $(c).val();
}
} else {
data[$key] = $t.val();
}
}
}
});
return data;
}
// , _showFormContent, ,
/*html :