作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将表序列化为 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/
我是一名优秀的程序员,十分优秀!