我有一个表,我可以在其中向上或向下移动行,我已经在 .现在我需要保存表的新顺序。我想我需要保存行[索引],但我没有使用 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>
关于javascript - JQuery 迭代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61265373/