jq提交数据格式

-一个大坑 2018-09-21 11:07:07

<form id="paymentItemFrom" onsubmit="return false;">
<div class="row">
@for(int i=1; i<4; i++)
{
<div class="col-sm-12 row-top">
<div class="input-group">
<span class="input-group-addon">X坐標(px)</span>
<input type="text" class="form-control empTypeahead" name="X"/>
</div>
</div>
<div class="col-sm-12 row-top">
<div class="input-group">
<span class="input-group-addon">Y坐標(px)</span>
<input type="text" class="form-control empTypeahead" name="Y"/>
</div>
</div>
<div class="col-sm-12 row-top">
<div class="input-group">
<span class="input-group-addon">寬度(px)</span>
<input type="text" class="form-control empTypeahead" name="Width" />
</div>
</div>
<div class="col-sm-12 row-top">
<div class="input-group">
<span class="input-group-addon">高度(px)</span>
<input type="text" class="form-control empTypeahead" name="Height"/>
</div>
</div>
}
</div>
</form>


我想提交成这样的json要怎么写?
[{"X":50,"Y":90,"Width":300,"Height":200},
{"X":50,"Y":90,"Width":200,"Height":300},
{"X":30,"Y":80,"Width":200,"Height":200}]

用serializeArray()变成一堆[object Object]
...全文
257 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员的键盘 2018-09-21
  • 打赏
  • 举报
回复
引用 4 楼 happy4944 的回复:
[quote=引用 2 楼 qq_35955916 的回复:]
按照你的html格式 可以这么写

var [X,Y,Width,Height]=[document.querySelectorAll("input[name='X']"),document.querySelectorAll("input[name='Y']"),document.querySelectorAll("input[name='Width']"),document.querySelectorAll("input[name='Height']")];
var arry=[];
[...X].forEach((item,index)=>{arry.push({X:item.value,Y:Y[index].value,Width:Width[index].value,Height:Height[index].value}) });

如果需要把arry转成json字符串 可以用 JSON.stringify(arry)

form表单可以直接调用某个方法提交成这样的json吗
[{"X":50,"Y":90,"Width":300,"Height":200},
{"X":50,"Y":90,"Width":200,"Height":300},
{"X":30,"Y":80,"Width":200,"Height":200}][/quote]
大概form表单,不能直接映射(我是不知道怎么玩),你用ajax提交看看吧
https://blog.csdn.net/hanjun0612/article/details/50441437
-一个大坑 2018-09-21
  • 打赏
  • 举报
回复
引用 3 楼 u013116426 的回复:
写的太时髦了,估计理解不了
可以看看http://es6.ruanyifeng.com/#docs/array
如果楼主要做兼容ie的话这个就有点蛋疼了。

es6ie不兼容
-一个大坑 2018-09-21
  • 打赏
  • 举报
回复
引用 2 楼 qq_35955916 的回复:
按照你的html格式 可以这么写

var [X,Y,Width,Height]=[document.querySelectorAll("input[name='X']"),document.querySelectorAll("input[name='Y']"),document.querySelectorAll("input[name='Width']"),document.querySelectorAll("input[name='Height']")];
var arry=[];
[...X].forEach((item,index)=>{arry.push({X:item.value,Y:Y[index].value,Width:Width[index].value,Height:Height[index].value}) });

如果需要把arry转成json字符串 可以用 JSON.stringify(arry)

form表单可以直接调用某个方法提交成这样的json吗
[{"X":50,"Y":90,"Width":300,"Height":200},
{"X":50,"Y":90,"Width":200,"Height":300},
{"X":30,"Y":80,"Width":200,"Height":200}]
___紫菜 2018-09-21
  • 打赏
  • 举报
回复
引用 2 楼 qq_35955916 的回复:
按照你的html格式 可以这么写

var [X,Y,Width,Height]=[document.querySelectorAll("input[name='X']"),document.querySelectorAll("input[name='Y']"),document.querySelectorAll("input[name='Width']"),document.querySelectorAll("input[name='Height']")];
var arry=[];
[...X].forEach((item,index)=>{arry.push({X:item.value,Y:Y[index].value,Width:Width[index].value,Height:Height[index].value}) });

如果需要把arry转成json字符串 可以用 JSON.stringify(arry)

写的太时髦了,估计理解不了
可以看看http://es6.ruanyifeng.com/#docs/array
如果楼主要做兼容ie的话这个就有点蛋疼了。
程序员的键盘 2018-09-21
  • 打赏
  • 举报
回复
按照你的html格式 可以这么写

var [X,Y,Width,Height]=[document.querySelectorAll("input[name='X']"),document.querySelectorAll("input[name='Y']"),document.querySelectorAll("input[name='Width']"),document.querySelectorAll("input[name='Height']")];
var arry=[];
[...X].forEach((item,index)=>{arry.push({X:item.value,Y:Y[index].value,Width:Width[index].value,Height:Height[index].value}) });

如果需要把arry转成json字符串 可以用 JSON.stringify(arry)
jk_ 2018-09-21
  • 打赏
  • 举报
回复
应该转换成json吧?

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧