如何在编辑HTML表后刷新它

How do I refresh a HTML table after editing it?

本文关键字:刷新 HTML 编辑      更新时间:2023-09-26

我正在制作一个非常简单的系统来记录预订,除了"预订号"answers"价格"部分是静态的之外,其他部分都是可编辑的。预订号码是预定义的,价格是一个变量,取决于"所选座位数"列的值。当我打开.html文件时,"price"列总是显示为未定义,并且我希望在相应地编辑了"number of seats selected"之后刷新代码。乘以的价格也是预定义的和静态的,这意味着当选择的座位数量已经定义时,它将乘以5。这是我的代码:

<script type="text/javascript">
var seatvar = document.getElementsByClassName("seatchosen");
var pricepay = seatvar * 5
</script>
<script type="text/javascript">
function RefreshTable () {
            var table = document.getElementById ("bookingtable");
            table.refresh ();
        }
</script>
<table class="editabletable" id="bookingtable" border="1">
<tr><th>Booking Number</th>
<th>Name</th>
<th>Number of Seats Chosen</th>
<th>Seats Chosen</th>
<th>Price</th>
</tr>
<tr><td>1</td>
<td><div contenteditable></div></td>
<td class="seatchosen"><div contenteditable></div></td>
<td><div contenteditable></div></td>
<td><script type="text/javascript">document.write(pricepay);</script></td></tr>
</table>
<p>Price Per Seat: £5</p>
<button onclick="RefreshTable()">Refresh the table</button>

该表只显示了一行,尽管有二十行,但它们都遵循相同的代码。我也包括了标题。我不明白为什么在我编辑了所选座位数量的值后,它不令人耳目一新。

试试这个代码,

把你的id放在成功函数的位置。

JS

$('.editabletable').click(function () {
    $.ajax({
        url: $(this).data("url"),
        type: 'GET',
        cache: false,
        success: function (result) {
            $('#your_id').html(result);
        }
    });
    return false;
});

首先,

getElementsByClassName返回元素数组。所以var seatvar = document.getElementsByClassName("seatchosen");之后seatvar将不包含总座位。

其次,

点击按钮后,您必须计算价格和座位。这是在RefreshTable()函数中,因此它的值将在单击后更新。

这可能会对你有所帮助。。

<script type="text/javascript">
function RefreshTable () {
    for (i = 1; i <= totalRows; i++) {
        var seatvar =  document.getElementsById("seatchosen_1").value;
        var pricepay = seatvar * 5; 
        document.getElementById('price_1').innerHTML = pricepay;
    }
}
</script>
<table class="editabletable" id="bookingtable" border="1">
<tr>
    <th>Booking Number</th>
    <th>Name</th>
    <th>Number of Seats Chosen</th>
    <th>Seats Chosen</th>
    <th>Price</th>
</tr>
<tr>
    <td>1</td>
    <td><div contenteditable></div></td>
    <td class="seatchosen">3 <input type="hidden" name="seatchosen_1" value="3"></td>
    <td><div contenteditable></div></td>
    <td id="price_1"> </td>
</tr>
<tr>
    <td>2</td>
    <td><div contenteditable></div></td>
    <td class="seatchosen">5 <input type="hidden" name="seatchosen_2" value="5"></td>
    <td><div contenteditable></div></td>
    <td id="price_2"> </td>
</tr>
.
.
.
.
</table>
<p>Price Per Seat: £5</p>
<button onclick="RefreshTable()">Refresh the table</button>

命令后:

var seatvar = document.getElementsByClassName("seatchosen");

当你尝试在控制台中打印可变seatvar时,如

console.log(seatvar)

您将得到结果[object HTMLCollection],因为您不能反对多个数字,只有数字*数字