js把一个form表单转换成一个form表单对象

这是一个form:

<form id="searchUserForm" action="" method="post">
	<table id="searchUserTable" align="left" style="margin-top: 15px;" cellpadding="5px;">
		<tr>
			<td>用户名:<input type="text" name="username"/></td>
			<td>开始时间:<input type="text" name="starttime" class="easyui-datetimebox" editable="false" style="width: 150px;"/></td>
			<td>结束时间:<input type="text" name="endtime" class="easyui-datetimebox" editable="false" style="width: 150px;"/></td>
			<td><a id="searchUser" class="easyui-linkbutton">查询</a></td>
			<td><a id="resetBth" class="easyui-linkbutton">清空</a></td>
		</tr>
	</table>
</form>

目标是把form表单的数据转换成一个js对象,这样方便其他操作

	//js方法:序列化表单 ,把表单传递进来,就可以得到一个表单的js对象,如$("#userForm");		
	function serializeForm(form){
		var obj = {};
		$.each(form.serializeArray(),function(index){
			if(obj[this['name']]){
				obj[this['name']] = obj[this['name']] + ','+this['value'];
			} else {
				obj[this['name']] =this['value'];
			}
		});
		return obj;
	}

 

posted @ 2017-07-29 12:04  半生戎马,共话桑麻、  阅读(1767)  评论(0)    收藏  举报
levels of contents