jquery form序列化 转为json对象
页面:
写道
<form id="cardForm">
<div class="selBox2" style="width: 760px;">
<select class="sel fl" id="cardSelectDate" name="cardSelectDate">
<option value="0">请选择</option>
<option value="1">今天</option>
<option value="2">昨天</option>
<option value="3">近七天</option>
<option value="4">近一个月</option>
</select>
<div class="userAuto fl">
<span>自定义时间</span><input type="text" id="startdate" name="startdate" readonly="readonly" class="inpW3 inp datepicker" />--<input id="enddate" name="enddate" type="text" readonly="readonly" class="inpW3 inp datepicker" />
</div>
<div class="seacBtn btnWidth5"><a href="#" onclick="queryValidateCardList();">查 询</a></div>
<div class="seacBtn btnWidth5"><a href="#" onclick="resetQuery('cardForm');">重 置</a></div>
</div>
?我只是写个例子,点击重置。
js:
/**
* 重置form表单
* @param formId form的id
*/
function resetQuery(formId){
var fid = "#" + formId;
var str = $(fid).serialize();
//str= cardSelectDate=3&startdate=2012-02-01&enddate=2012-02-04
var ob= strToObj(str);
alert(ob.startdate);//2012-02-01
}
function strToObj(str){
str = str.replace(/&/g,"','");
str = str.replace(/=/g,"':'");
str = "({'"+str +"'})";
obj = eval(str);
return obj;
}
?