如何将表序列化为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>
答案 0 :(得分:2)
您可以将nth-child
选择器与n+2
一起使用,仅选择tr&gt; = 2:
#myForm tbody tr:nth-child(n+2)
但是,结果将不是一个对象数组,其中每个对象都是一个行的对象。结果将是一个对象数组,其中每个select / input / textarea本身就是一个对象。
您可以使用each()
上的trs
功能覆盖所有这些功能,以获得预期效果。
以下是两个选项的示例:
$(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)
});
});
&#13;
<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>
&#13;
答案 1 :(得分:0)
您可以使用SerializeFromTable JQuery插件序列化任何具有或不具有表单元素的表。
无需用