form表单的部分数据转json格式

部门名称、报账信息(金额、资料、说明)这两个是一个表单里的,怎么让报账信息输出来是一个json格式呢,而其他数据不需要转变json格式?难道是form嵌套form?


慕尼黑的夜晚无繁华
浏览 484回答 1
1回答

qq_笑_17

假设你在用jQuery,假设你需要一个json对象, 那么你可以试一下<form id="myForm">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="aa.a" value="a">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="aa.b" value="b">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="aa.c" value="c">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="bb.a" value="d">&nbsp; &nbsp; </form>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <input type="button" value="biu" id="biu">$(document).ready(function() {&nbsp; &nbsp; $('#biu').click(function() {&nbsp; &nbsp; &nbsp; &nbsp; var obj = getJson('aa');&nbsp; &nbsp; &nbsp; &nbsp; console.log(obj);&nbsp; &nbsp; });});function getJson(prev) {&nbsp; &nbsp; var items = $('#myForm input[name^=' + prev + ']');&nbsp; &nbsp; if (null != items && items.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; var obj = {};&nbsp; &nbsp; &nbsp; &nbsp; $.each(items, function(k, v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var that = $(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[that.attr('name')] = that.val();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return obj;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript