JQuery遍历一个表



我有一个表,可以在其中向上或向下移动我在上面加了id的行。现在我需要保存表的新顺序。我想我需要保存行[索引],但我没有使用jquery的经验。我的表是id="rtbl",调用函数的按钮是"sroute"。我在函数中有一个警报,但它不起作用。刚接触jquery,我需要做些什么才能让它工作起来?

$("#sroute").click(function(){
alert("Hello")
$("#rtbl tr").each(function() {
var val1 = $(t.rows[i].cells[0]).text();
alert(val1) ;
i++;
});
});
<button id="sroute">Save Order</button>
<table id='rtbl'>
<tr><th>Invoice</th></tr>
<tr id='789'><td>789</td></tr>
<tr id='123'><td>123</td></tr>
<tr id='456'><td>456</td></tr>
</table>

因此,目标是更新数据库字段"stopnum",其中发票789 stopnum将为1,发票123 stopnum为2,发票456 stopnum中将为3。

数据库更新API所需对象的粗略估计。你必须对此进行调整。

window.onload = (function(){
document.getElementById('sroute').onclick=( function(){
var arrayOfInvoices = $('#rtbl tr:not(:eq(0))').map( function(i,el) { return ({ invoice: el.id, stopnum: i+1 }) } ).get();
alert(JSON.stringify(arrayOfInvoices));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="sroute">Save Order</button>
<table id='rtbl'>
<tr><th>Invoice</th></tr>
<tr id='789'><td>789</td></tr>
<tr id='123'><td>123</td></tr>
<tr id='456'><td>456</td></tr>
</table>

最新更新