下面是动态生成的字段。为了简单起见,我删除了不必要的代码。单击按钮时,请帮助我生成一个JSON对象数组
<div class="row sertile">
<input type="hidden" class="form-control parType" value="Service">
<input type="hidden" class="form-control id" value="144">
<input type="hidden" class="form-control serviceName" value="Full face">
<input type="hidden" class="form-control mrp" value="200">
<input type="hidden" class="form-control dis" value="0">
<input type="hidden" class="form-control dur" value="30">
</div>
<div class="row sertile">
<input type="hidden" class="form-control parType" value="Service">
<input type="hidden" class="form-control id" value="124">
<input type="hidden" class="form-control serviceName" value="Half face">
<input type="hidden" class="form-control mrp" value="200">
<input type="hidden" class="form-control dis" value="0">
<input type="hidden" class="form-control dur" value="30">
</div>我需要如下输出:
[
{
parType: 'Service',
id: 144,
serviceName: 'Full Face',
...
},
{
parType: 'Service',
id: 124,
serviceName: 'Half Face',
...
},
...
]发布于 2021-11-25 11:41:49
我已经找到了解决这个问题的方法。感谢大家的帮助。
function createJSON() {
jsonObj = [];
$(".sertile").each(function() {
var id = $(this).find("#serId").val();
var name = $(this).find("#serName").val();
item = {}
item["title"] = id;
item["email"] = name;
jsonObj.push(item);
});
console.log(jsonObj);
}发布于 2021-11-25 11:34:44
你试过使用Element.getElementsByClassName()吗?
一种想法可能是在div中添加一个标记,并通过文档上的getElementsByClassName获取它们,然后循环遍历它们以按其类获取每个字段,并将其添加到一个对象中,然后如果需要,可以将其转换为string json
https://stackoverflow.com/questions/70110049
复制相似问题