gpt4 book ai didi

javascript - 如何将表行序列化为json对象

转载 作者:行者123 更新时间:2023-11-29 19:09:31 25 4
gpt4 key购买 nike

如何将表序列化为 json 数组,以便每个数组元素都包含代表一个表行的 json 对象:

[
{ name: "variable1", valuetostore: "a-b", totaltype: "Lowest" },
{ name: "variable2", valuetostore: "c-d", totaltype: "Highest" }
]

我尝试了下面的代码,但这会生成具有名称和值属性的对象,并且数组中的成员多于表行中的成员。

它还序列化隐藏的第一行。它是用于添加行的模板行,不应出现在结果中。

$(function() {
$("#btnShow").on("click", function() {
console.log($("#myForm").serializeArray());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="modal" id="reportVariables" tabindex="-1" role="dialog" aria-labelledby="reportVariablesLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-body">
<form id="myForm">
<table class="table table-condensed table-striped" id="reportVariablesTable">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Calculate</th>
</tr>
</thead>
<tbody>
<!-- table contains one hidden rows which should not -->
<tr style='display:none'>
<td>
<input type="text" name="name">
</td>

<td>
<textarea name="valuetostore"></textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest">Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>

<!-- other are data rows which should sent -->
<tr>
<td>
<input type="text" name="name" value="variable1">
</td>

<td>
<textarea name="valuetostore">a-b</textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" selected>Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="variable2">
</td>

<td>
<textarea name="valuetostore">c-d</textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" >Smallest</option>
<option value="Highest" selected>Biggers</option>
</select>
</td>
</tr>

</tbody>
</table>
<button id="btnShow" type="button">
Show
</button>
</form>

</div>
</div>
</div>
</div>

最佳答案

您可以使用 nth-child 选择器和 n+2 来只选择 tr>=2:

#myForm tbody tr:nth-child(n+2)

但是结果不会是一个对象数组,其中每个对象都是一行的对象。结果将是一个对象数组,其中每个选择/输入/文本区域本身就是一个对象。

您可以在 trs 上使用 each() 函数遍历所有这些以获得预期的结果。

这是两个选项的示例:

$(function() {
$("#btnShow1").on("click", function() {
console.log($("#myForm tbody tr:nth-child(n+2) textarea,#myForm tbody tr:nth-child(n+2) input,#myForm tbody tr:nth-child(n+2) select").serializeArray());
});

$("#btnShow2").on("click", function() {
var ar = [];
$("#myForm tbody tr:nth-child(n+2)").each(function() {
rowData = $(this).find('input, select, textarea').serializeArray();
var rowAr = {};
$.each(rowData, function(e, v) {
rowAr[v['name']] = v['value'];
});
ar.push(rowAr);
});
console.log(ar)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="modal" id="reportVariables" tabindex="-1" role="dialog" aria-labelledby="reportVariablesLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-body">
<form id="myForm">
<table class="table table-condensed table-striped" id="reportVariablesTable">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Calculate</th>
</tr>
</thead>
<tbody>
<!-- table contains one hidden rows which should not -->
<tr style='display:none'>
<td>
<input type="text" name="name">
</td>

<td>
<textarea name="valuetostore"></textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest">Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>

<!-- other are data rows which should sent -->
<tr>
<td>
<input type="text" name="name" value="variable1">
</td>

<td>
<textarea name="valuetostore">a-b</textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" selected>Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="variable2">
</td>

<td>
<textarea name="valuetostore">c-d</textarea>
</td>

<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" >Smallest</option>
<option value="Highest" selected>Biggers</option>
</select>
</td>
</tr>

</tbody>
</table>
<button id="btnShow1" type="button">
Options #1
</button><br />
<button id="btnShow2" type="button">
Options #2
</button>
</form>

</div>
</div>
</div>
</div>

关于javascript - 如何将表行序列化为json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40241115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com