使用 jQuery.form.serialize() 获取表单数据后转成 json
2018-01-24 16:45
961 查看
HTML:
<form action="#" method="post">
<div>
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div>
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</div>
<p>
<input type="submit" value="Send" />
</p>
</form>
JavaScript:
(function ($) {
$.fn.serializeFormJSON = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
$('form').submit(function (e) {
e.preventDefault();
var data = $(this).serializeFormJSON();
console.log(data);
/* Object
email: "value"
name: "value"
password: "value"
*/
});
转载网址:https://jsfiddle.net/gabrieleromanato/bynaK/
如有侵权,请联系删除!
<form action="#" method="post">
<div>
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div>
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</div>
<p>
<input type="submit" value="Send" />
</p>
</form>
JavaScript:
(function ($) {
$.fn.serializeFormJSON = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
$('form').submit(function (e) {
e.preventDefault();
var data = $(this).serializeFormJSON();
console.log(data);
/* Object
email: "value"
name: "value"
password: "value"
*/
});
转载网址:https://jsfiddle.net/gabrieleromanato/bynaK/
如有侵权,请联系删除!
相关文章推荐
- 使用jQuery-Form做表单submit提交,获取响应json数据
- jquery序列化form表单使用ajax提交后处理返回的json数据
- jquery的$('#theForm').serialize()获取表单数据,ajax传递到服务器中文乱码。
- jquery序列化form表单使用ajax提交后处理返回的json数据
- 将form表单里的数据封装成对象 json2.js的例子 jquery的serialize方法转换空格为+号的解决方法
- jquery序列化form表单使用ajax提交后处理返回的json数据
- jQuery使用serialize(),serializeArray()方法取得表单数据+字符串和对象类型两种表单提交的方法
- jQuery中使用Ajax获取JSON格式数据示例代码
- jQuery的serialize方法无法获取form数据
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理
- 使用jQuery的formValidator框架对表单数据实现前台校验
- C# 把数据集生成为JSON格式 并在使用Jquery 获取JSON数据
- jQuery 使用 $.getJSON() 跨域获取 JSON 数据
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- 使用前端使用jQuery获取 在apsx页面的page_load方法直接返回json数据
- jQuery通过ajax获得后台json数据给form表单赋值
- jQuery中使用Ajax获取JSON格式数据
- IT忍者神龟之jQuery 使用 $.getJSON() 跨域获取 JSON 数据
- 使用jQuery解析JSON数据(jquery在前台对从后台获取到的json数据进行解析,并进行显示)
- 使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据