亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在表上創建新行時功能不起作用

在表上創建新行時功能不起作用

手掌心 2021-11-18 09:54:17
我有一個 html 表,當用戶單擊按鈕時,我會在其中動態添加行。我的表格每行的兩個單元格的編號應該相乘。我做了一個可以乘以 2 個單元格數字的函數,但是當我通過按下add button該函數創建一個新行時,該函數在新行上不起作用,它總是在表格的第一行上起作用。這是 HTML 代碼:<div class="table-responsive container">    <table class="table">        <thead class="dark">        <tr>            <th scope="col">SL.</th>            <th scope="col">Item Description</th>            <th scope="col">Price</th>            <th scope="col">Qty.</th>            <th scope="col">Total</th>            <th scope="col">Del</th>        </tr>        </thead>        <tbody>        <tr>            <th scope="row">1</th>            <td><label>                <input type="text" name="name">            </label>            </td>            <td><label>                <input type="text" id="price" oninput="calculate()">            </td>            <td>                <input type="text" id="qt" oninput="calculate()">            </td>            <td>                <input type="text" id="ttl" name='total'>            </td>            <td>                <button class="remove">-</button>            </td>        </tr>        </tbody>    </table>    <button id="addRow">+ Add</button></div>這是 JavaScript 代碼:let temp_td = '<tr>' +    '<th scope="row">1</th>' +    '<td><label><input type="text" name="name"></label></td>' +    '<td><label><input type="text" id="price" oninput="calculate()"></td>' +    '<td><input type="text" id="qt" oninput="calculate()"></td>' +    '<td><input type="text" id="ttl" name="total"></td>' +    '<td><button class="remove">-</button></td>' +'</tr>';$(function () {    $('tbody').sortable();    $('#addRow').click(function () {        $('tbody').append(temp_td)    });    $(document).on('click', '.remove', function () {        $(this).parents('tr').remove();    });    $('#getValues').click(function () {        const total = [];        let sum = 0;        $('input[name="total"]').each(function (i, elem) {            total.push($(elem).val())        });我試圖改變很多東西,但它不起作用。
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

您不能在 HTML 中復制 ID。您可以將行號保存在每次添加新行時遞增的變量中,并使用該變量生成 ID:


$(function() {

  let row = 1;


  $('#addRow').click(function() {

    row++;

    let temp_td = '<tr><th scope="row">' + row + '</th><td><label><input type="text" name="name"></label></td><td><label><input type="text" id="price' + row + '" oninput="calculate(' + row + ')"></td><td><input type="text" id="qt' + row + '" oninput="calculate(' + row + ')"></td><td><input type="text" id="ttl' + row + '" name="total"></td><td><button class="remove">-</button></td></tr>';

    $('tbody').append(temp_td)

  });


  $(document).on('click', '.remove', function() {

    $(this).parents('tr').remove();

  });

});


function calculate(r) {

  var price = document.getElementById('price' + r).value;

  var qt = document.getElementById('qt' + r).value;

  var ttl = document.getElementById('ttl' + r);

  var multiply = price * qt;

  ttl.value = multiply;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<div class="table-responsive container">

  <table class="table">

    <thead class="dark">

      <tr>

        <th scope="col">SL.</th>

        <th scope="col">Item Description</th>

        <th scope="col">Price</th>

        <th scope="col">Qty.</th>

        <th scope="col">Total</th>

        <th scope="col">Del</th>

      </tr>

    </thead>

    <tbody>

      <tr>

        <th scope="row">1</th>

        <td><label>

                    <input type="text" name="name">

                </label>

        </td>

        <td><label>

                    <input type="text" id="price1" oninput="calculate(1)">

                </td>

                <td>

                    <input type="text" id="qt1" oninput="calculate(1)">

                </td>

                <td>

                    <input type="text" id="ttl1" name='total'>

                </td>

                <td>

                    <button class="remove">-</button>

                </td>

            </tr>

            </tbody>

        </table>

        <button id="addRow">+ Add</button>


如果您愿意,您也可以對名稱執行此操作。我在上面的代碼中沒有這樣做,因為它對于這個答案不是必需的。


或者,您可以將元素本身傳遞給calculate()函數,然后使用 jQuery 查找其他“兄弟”元素。所以你根本不需要身份證。


查看完整回答
反對 回復 2021-11-18
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號