In my code below user choose a color and types customer name and adds the data to a dynamic table. My problem is that I want to save my table in JSON but my code saves only the first row of the table and it doesn't adds rest of the rows
function colorddRow() {
var colorpartsrows = "";
var colparts = $("#color-part-list").val();
var customer = $("#customer-name").val();
var colpartssum = $("#color-part-list :selected").length;
colorpartsrows +=
"<tr><td>" +
colparts +
"</td><td>" +
customer +
"</td><td>" +
colpartssum +
"</td></tr>";
$(colorpartsrows).appendTo("#dataTable1-color-parts tbody");
}
function showValues() {
var fields = $(":input").serializeArray();
$("#results").empty();
jQuery.each(fields, function(i, field) {
$("#results").append(field.value + " ");
});
}
$("select").change(showValues);
showValues();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th name="color[]" id="color">Color</th>
<th name="Customer[]" id="customer">Color</th>
</tr>
<tr>
<td id="color-des">
<select id="color-part-list" data-placeholder="Rejected Parts">
<option value="Red">Red</option>
<option value="Grey">Grey</option>
<option value="Blue">Blue</option>
<option value="Yellow">Yellow</option>
<option value="White">White</option>
<option value="Black">Black</option>
</select>
</td>
<td id="customer">
<input id="customer-name" type="text" name="customer">
</td>
</tr>
</table>
<input type="button" value="Add The Color" onclick="colorddRow()" />
<table id="dataTable1-color-parts">
<tr>
<th>Color</th>
<th>Customer Name</th>
</tr>
</table>
<button type="button" onclick="showValues()">Save to JSON</button>
question from:
https://stackoverflow.com/questions/65896460/save-a-dynamic-html-table-in-json 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…