使用 JavaScript 按钮后重置表行

所以我有以下代码:


$('input.qty').change(function() {

  var $tr = $(this).closest('tr');

  var price = $(this).closest('tr').find('input.price').val();

  // var price = parseFloat($tr.find('td').eq(1).text());

  var qty = parseInt($(this).val());

  $(this).closest('tr').find('input.amt').val(qty * price);

});

.tis {

  border: none;

  background-color: Transparent;

}

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

<!doctype html>



<html>


  <head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">


    <!-- Font Awesome CDN -->

    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">


    <!-- Bootstrap CSS/CDN -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


   

  


  </head>


  <body>


    <main role="main">


      <script type="text/javascript">

      

      function addRow() {

 var table = document.getElementById("invoiceTableBody")

 table.innerHTML+=

 '<tr>' +

   '<td>'+

     '<div>'+

       '<select class="try" name="this">'+


       '<option value="Option1">Option 1</option>'+

       '<option value="Option2">Option 2</option>'+

       '<option value="Option3">Option 3</option>'+


       '</select>'+

     '</div>'+

   '</td>'+

   '<td><input type="text" name="l108Price[]" size= "3" class="price tis"></td>' +

   '<td><input type="text" name="l108Qty[]" size= "1" class="qty tis"></td>' +

   '<td><input type= "text" name="l108Amt[]" size= "3" class="amt tis" disabled></td>' +

 '</tr>';

};

这里的目标是创建一个发票系统。一切工作正常,直到我尝试通过单击行末尾的按钮来添加行。一旦我这样做了,一切都会重置,我不知道为什么。



呼啦一阵风
浏览 69回答 2
2回答

繁华开满天机

该问题是由&nbsp;table.innerHTML += '<tr>...</tr>';&nbsp;引起的。声明。innerHTML更改对象的 HTML,这肯定会影响大小和位置,并且至少会触发部分回流。这就是为什么表行的内容将被重置的原因。而是使用&nbsp;HTMLElement.prototype.insertAdjacentHTML&nbsp;方法以正确的方式将 HTMLElement 插入到 DOM 中。所以我稍微重写了你的代码:$('#invoiceTableBody').on('change', 'input.qty', function() {&nbsp; var $tr = $(this).closest('tr');&nbsp; var price = $(this).closest('tr').find('input.price').val();&nbsp; // var price = parseFloat($tr.find('td').eq(1).text());&nbsp; var qty = parseInt($(this).val());&nbsp; $(this).closest('tr').find('input.amt').val(qty * price);});.tis {&nbsp; border: none;&nbsp; background-color: Transparent;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!doctype html><html><head>&nbsp; <!-- Required meta tags -->&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">&nbsp; <!-- Font Awesome CDN -->&nbsp; <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">&nbsp; <!-- Bootstrap CSS/CDN -->&nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"></head><body>&nbsp; <main role="main">&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; function addRow() {&nbsp; &nbsp; &nbsp; &nbsp; var table = document.getElementById("invoiceTableBody")&nbsp; &nbsp; &nbsp; &nbsp; // Composing your HTMLElement as a string&nbsp; &nbsp; &nbsp; &nbsp; var element =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<tr>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<div>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<select class="try" name="this">' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<option value="Option1">Option 1</option>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<option value="Option2">Option 2</option>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<option value="Option3">Option 3</option>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</select>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td><input type="text" name="l108Price[]" size= "3" class="price tis"></td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td><input type="text" name="l108Qty[]" size= "1" class="qty tis"></td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td><input type= "text" name="l108Amt[]" size= "3" class="amt tis" disabled></td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</tr>';&nbsp; &nbsp; &nbsp; &nbsp; // Inserting the string as a HTMLElement at the end of the table element&nbsp; &nbsp; &nbsp; &nbsp; table.insertAdjacentHTML('beforeend', element);&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; </script>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; <div class="col-md-8 offset-2">&nbsp; &nbsp; &nbsp; &nbsp; <table class="table thead-dark table-hover border-bottom" id="invoiceTable">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 60%">Item - Description</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 10%">Price</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 5%">Qty</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 10%">Amount</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style=>Action</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody id="invoiceTableBody">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr id="invoiceTableRow">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select class="try" name="this">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option1">Option 1</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option2">Option 2</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option3">Option 3</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="text" name="l108Price[]" size="3" class="price tis"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="text" name="l108Qty[]" size="1" class="qty tis"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type='text' name='l108Amt[]' size='3' class="amt tis" disabled></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </main>&nbsp; <!-- /role main --></body></html>如果您想将新行添加到您单击的行的正下方,可以使用下面的代码来完成。我从 HTML 中删除了你的脚本:$('#invoiceTableBody').on('click', 'button.action', function() {&nbsp; var row = event.target.closest(".invoiceTableRow");&nbsp; // Composing your HTMLElement as a string&nbsp; var element =&nbsp; &nbsp; '<tr class="invoiceTableRow">' +&nbsp; &nbsp; '<td>' +&nbsp; &nbsp; '<div>' +&nbsp; &nbsp; '<select class="try" name="this">' +&nbsp; &nbsp; '<option value="Option1">Option 1</option>' +&nbsp; &nbsp; '<option value="Option2">Option 2</option>' +&nbsp; &nbsp; '<option value="Option3">Option 3</option>' +&nbsp; &nbsp; '</select>' +&nbsp; &nbsp; '</div>' +&nbsp; &nbsp; '</td>' +&nbsp; &nbsp; '<td><input type="text" name="l108Price[]" size= "3" class="price tis"></td>' +&nbsp; &nbsp; '<td><input type="text" name="l108Qty[]" size= "1" class="qty tis"></td>' +&nbsp; &nbsp; '<td><input type= "text" name="l108Amt[]" size= "3" class="amt tis" disabled></td>' +&nbsp; &nbsp; '<td><div><button type="button" name="button" style="background-color:Transparent; border:none; color:green;" class="action"><i class="fas fa-plus-circle"></i></button></div></td>' +&nbsp; &nbsp; '</tr>';&nbsp; // Inserting the string as a HTMLElement after the current row&nbsp; row.insertAdjacentHTML('afterend', element);});$('#invoiceTableBody').on('change', 'input.qty', function() {&nbsp; var $tr = $(this).closest('tr');&nbsp; var price = $(this).closest('tr').find('input.price').val();&nbsp; // var price = parseFloat($tr.find('td').eq(1).text());&nbsp; var qty = parseInt($(this).val());&nbsp; $(this).closest('tr').find('input.amt').val(qty * price);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><html><head>&nbsp; <!-- Required meta tags -->&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">&nbsp; <!-- Font Awesome CDN -->&nbsp; <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">&nbsp; <!-- Bootstrap CSS/CDN -->&nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"></head><body>&nbsp; <main role="main">&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; </script>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; <div class="col-md-8 offset-2">&nbsp; &nbsp; &nbsp; &nbsp; <table class="table thead-dark table-hover border-bottom" id="invoiceTable">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 60%">Item - Description</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 10%">Price</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 5%">Qty</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 10%">Amount</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style=>Action</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody id="invoiceTableBody">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr id="invoiceTableRow" class="invoiceTableRow">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select class="try" name="this">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option1">Option 1</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option2">Option 2</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="<Option3">Option 3</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="text" name="l108Price[]" size="3" class="price tis"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="text" name="l108Qty[]" size="1" class="qty tis"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type='text' name='l108Amt[]' size='3' class="amt tis" disabled></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div><button type="button" name="button" class="action" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </main>&nbsp; <!-- /role main --></body></html>

Helenr

您犯了一个非常简单的错误,请在此处检查您的代码:var table = document.getElementById("invoiceTableBody")&nbsp;table.innerHTML+=&nbsp;'<tr>' +&nbsp; &nbsp;'<td>'+&nbsp; &nbsp; &nbsp;'<div>'+&nbsp; &nbsp; &nbsp; &nbsp;'<select class="try" name="this">'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option1">Option 1</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option2">Option 2</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option3">Option 3</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'</select>'+&nbsp; &nbsp; &nbsp;'</div>'+&nbsp; &nbsp;'</td>'+&nbsp; &nbsp;'<td><input type="text" name="l108Price[]" size= "3" class="price tis"></td>' +&nbsp; &nbsp;'<td><input type="text" name="l108Qty[]" size= "1" class="qty tis"></td>' +&nbsp; &nbsp;'<td><input type= "text" name="l108Amt[]" size= "3" class="amt tis" disabled></td>' +&nbsp; &nbsp;'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>'; +&nbsp;'</tr>';这一行不应该有一个分号:'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>'; +您应该将该行更改为:'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>'+所以你的集团应该是这样的:var table = document.getElementById("invoiceTableBody")&nbsp;table.innerHTML+=&nbsp;'<tr>' +&nbsp; &nbsp;'<td>'+&nbsp; &nbsp; &nbsp;'<div>'+&nbsp; &nbsp; &nbsp; &nbsp;'<select class="try" name="this">'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option1">Option 1</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option2">Option 2</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'<option value="Option3">Option 3</option>'+&nbsp; &nbsp; &nbsp; &nbsp;'</select>'+&nbsp; &nbsp; &nbsp;'</div>'+&nbsp; &nbsp;'</td>'+&nbsp; &nbsp;'<td><input type="text" name="l108Price[]" size= "3" class="price tis"></td>' +&nbsp; &nbsp;'<td><input type="text" name="l108Qty[]" size= "1" class="qty tis"></td>' +&nbsp; &nbsp;'<td><input type= "text" name="l108Amt[]" size= "3" class="amt tis" disabled></td>' +&nbsp; &nbsp;'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>' +&nbsp;'</tr>';
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5