这是一个基本的超文本标记语言表格,通过使用lodash _.template来转储正文。Html表是:
<div id="attribute-values-wrapper">
<div class="table-responsive">
<table class="options table table-bordered">
<thead>
<tr>
<th></th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody id="attribute-values">
</tbody>
</table>
</div>
</div>Lodash _.template为:
<script type="text/html" id="attribute-value-template">
<tr>
<td class="text-center">
<span class="drag-icon">
<i class="fa"></i>
<i class="fa"></i>
</span>
</td>
<td>
<input type="hidden" name="values[<%- valueId %>][id]"
value="<%- value.id %>">
<div class="form-group">
<input type="text" name="values[<%- valueId %>][value]"
value="<%- value.value %>" class="form-control">
</div>
</td>
<td class="text-center">
<button type="button" class="btn btn-default delete-row"
data-toggle="tooltip" data-title="Delete Value">
<i class="fa fa-trash"></i>
</button>
</td>
</tr>
</script>简单的javascript函数来转储tbody。
let valuesCount = 0;
function addAttributeValue(value = {id: '', value: ''}) {
let template = _.template($('#attribute-value-template').html());
let html = template({valueId: valuesCount++, value});
$('#attribute-values').append(html);
}发布于 2020-12-01 14:15:56
我的问题的答案。
let attribute_values = [];
$('#attribute-values tr').each(function (index, element) {
let inputs = $(this).find('input'), o = {};
inputs.each(function () {
let name = $(this).attr('name');
if (name === "values[" + index + "][id]") {
o["id"] = this.value;
} else if (name === "values[" + index + "][value]") {
o["value"] = this.value;
}
});
attribute_values.push(o);
});https://stackoverflow.com/questions/65079248
复制相似问题