下面是拼装json 和 array 的两种情况,这里都是一维的。
array --
/* * 万能表单提交 针对于项目 * * parm:id 为表单id * */ function go_to_from($id){ var kv = deal_from($id); //解析数据 for (x in kv) { alert(x); alert(kv[x]) } } //万能表单数据处理 /* * 表单查询一般是text search select 类型 * */ function deal_from(id){ var kv = new Array(); $("#"+id).find('input').each(function(){ //构造数组 $type = $(this).attr('type'); switch($type) { case "search": $v = $(this).val(); $k = $(this).attr('name'); kv[$k]= $v; break; case "text": $v = $(this).val(); $k = $(this).attr('name'); kv[$k]= $v; break; case "select": $k = $(this).attr('name'); $v = $(this).find('option:selected').val(); kv[$k]= $v; break; case "button": break; default: alert("出现了程序未曾预料的情况") return; } }); //去掉最后一个逗号 return kv; }
json --
* * 万能表单提交 针对于项目 * * parm:id 为表单id * */ function go_to_from($id){ $json = deal_from($id); //解析json数据 $json = $.parseJSON($json); for( x in $json){ alert(x); alert($json[x]); } } //万能表单数据处理 /* * 表单查询一般是text search select 类型 * */ function deal_from(id){ $json = "{"; $("#"+id).find('input').each(function(){ //构造json $type = $(this).attr('type'); switch($type) { case "search": $v = $(this).val(); $k = $(this).attr('name'); $json += '"'+$k+'":"'+$v+'",'; $kv[$k]= $v; break; case "text": $v = $(this).val(); $k = $(this).attr('name'); $json += '"'+$k+'":"'+$v+'",';break; case "select": $k = $(this).attr('name'); $v = $(this).find('option:selected').val(); $json += '"'+$k+'":"'+$v+'",'; break; case "button": break; default: alert("出现了程序未曾预料的情况") return; } }); //去掉最后一个逗号 $json = $json.substr(0,$json.length-1); $json +="}" return $json; }
此内容有误;