我正在尝试在表格下方的div中获取所有“供应商”及其所有值。
例如:
<td class="supplier" data-id="">Supplier1</td>
<td><input class="allocate_amount" id="allocate_amount" data-id="" type="text" name="allocate_amount" value="1000"></td>
<td class="supplier" data-id="">Supplier1</td>
<td><input class="allocate_amount" id="allocate_amount" data-id="" type="text" name="allocate_amount" value="3000"></td>
<td class="supplier" data-id="">Supplier2</td>
<td><input class="allocate_amount" id="allocate_amount" data-id="" type="text" name="allocate_amount" value="50"></td>
<td class="supplier" data-id="">Supplier2</td>
<td><input class="allocate_amount" id="allocate_amount" data-id="" type="text" name="allocate_amount" value="900"></td>
然后在表格后面
<div id="suppliers_list"></div>
我的代码:
var suppliertotal = [];
$(".supplier").each(function(){
var length = suppliertotal.length;
var supplier = $(this).text();
var supplierid = $(this).data('id');
var amount = Number($(".allocate_amount[data-id='" + supplierid + "']").val());
//alert(supplier);
//alert(supplierid);
//alert(amount);
//alert($.inArray(supplier, suppliertotal));
if($.inArray(supplier, suppliertotal) > -1){
var value = $.inArray(supplier, suppliertotal);
var x = value + 1;
var amount = suppliertotal[x];
amount += Number($(".allocate_amount[data-id='" + supplierid + "']").val());
suppliertotal.pop();
suppliertotal.pop();
suppliertotal.push(supplier, amount);
} else {
suppliertotal.push(supplier, amount);
}
$('#suppliers_list').html("<span>" + suppliertotal +"</span>" + "<br>");
console.log(suppliertotal);
});
我正在尝试将每个供应商添加到一个数组中,并继续添加他们的输入。目前我无法分割供应商总数,因此目前看起来像供应商1、4000,供应商2、950。