如何使用 JQuery 编辑和更新动态表中的值?

下面是用于将输入添加到动态表中的代码,并且还为每一行生成一个编辑按钮,我的问题是,当我单击特定行的编辑按钮时,如何将表中的值传递回输入字段并然后,当我单击更新行按钮时,根据对输入字段中的值所做的更改来更新特定行。


$("#btnAdd").on('click', function() {

  let row = '<tr> <td>' + $("#insert-name").val() + '</td> <td>' + $("#insert-surname").val() + '</td> <td>' + "edit" + '</td> </tr>'

  $('tbody').append(row);


  $('td:contains("edit")').html("<button type='button'>" + "edit" + "</button>").on('click', function() {


  });

});

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


<form>

  <div>

    <label for="insert-name">Name:</label>

    <input type="text" id="insert-name">

  </div>

  <div>

    <label for="insert-surname">Surname:</label>

    <input type="text" id="insert-surname">

  </div>

</form>


<button type="button" id="btnAdd">Add to Table</button>

<button type="button" id="btnUpdate">Update row</button>


<table>

  <thead>

    <tr>

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

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

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

    </tr>

  </thead>

  <tbody id="tbody"></tbody>

</table>


慕沐林林
浏览 104回答 2
2回答

慕娘9325324

检查这个(阅读 JS 评论)$("#btnAdd").on('click', function() {&nbsp; &nbsp; let row = '<tr> <td>' + $("#insert-name").val() + '</td> <td>' + $("#insert-surname").val() + '</td> <td>' + "edit" + '</td> </tr>'&nbsp; &nbsp; $('tbody').append(row);&nbsp; &nbsp; $('td:contains("edit")').html("<button type='button' class='edit'>" + "edit" + "</button>").on('click', function() {&nbsp; &nbsp; &nbsp; &nbsp; });});//--------------------------------------------------------//$(document).on("click",".edit",function(){ // Click function on class '.edit' (your appended button)&nbsp; &nbsp; var name = $(this).parents("tr").find("td:eq(0)").html(); // Search for 'name' depending on this edit button parent.&nbsp; &nbsp; var surname = $(this).parents("tr").find("td:eq(1)").html(); // Search for 'surname' depending on this edit button parent.&nbsp; &nbsp; var rowNumber = $(this).parents("tr").index() // Get index of this edit button parent 'row'.&nbsp; &nbsp; $("#edit-name").val(name); // Read the name and put it&nbsp; in '#edit-name' inside '.editArea'.&nbsp; &nbsp; $("#edit-surname").val(surname); // Read the surname and put it&nbsp; in '#edit-surname' inside '.editArea'.&nbsp; &nbsp; $(".saveEdits").attr("for",rowNumber); // Store this row index as attribute in '.saveEdits' button to be able to pass it to the other function that saves data.&nbsp; &nbsp; $(".editArea").fadeIn(300); // Show the edit box.});$(".saveEdits").click(function(){ // Function to save data&nbsp; var rowNumber = parseInt($(this).attr("for")); // Get the row number that we already define in the prev. function.&nbsp; &nbsp; $('td:eq(0)','tr:eq('+(rowNumber+1)+')').html($("#edit-name").val()); // Update 'td' content depending on the 'tr' index.&nbsp; &nbsp; $('td:eq(1)','tr:eq('+(rowNumber+1)+')').html($("#edit-surname").val()); // Update 'td' content depending on the 'tr' index.});$(".cancel").click(function(){ // Button to cancel edit.&nbsp; $("#edit-name").val(""); // Empty value.&nbsp; $("#edit-surname").val(""); // Empty value.&nbsp; $(".saveEdits").attr("for","0"); // Set row number to zero.&nbsp; $(".editArea").fadeOut(300); // Hide edit area.});.editArea{&nbsp; display:none;&nbsp; background:#fff;&nbsp; padding:10px;&nbsp; border:1px solid #ddd;&nbsp; border-radius:5px;&nbsp; box-shadow:0 0 0 #000;&nbsp; width:50%;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!DOCTYPE html><html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title></title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <label for="insert-name">Name:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="insert-name">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <label for="insert-surname">Surname:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="insert-surname">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form>&nbsp; &nbsp; <button type="button" id="btnAdd">&nbsp; &nbsp; &nbsp; Add to Table&nbsp; &nbsp; </button>&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Surname</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Edit</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; <tbody id="tbody">&nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='editArea'>&nbsp; &nbsp; &nbsp; <label>Name</label>&nbsp; &nbsp; &nbsp; <input type="text" id="edit-name">&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <label>Surname</label>&nbsp; &nbsp; &nbsp; <input type="text" id="edit-surname">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; <button class='saveEdits' for="0">Save edits</button>&nbsp; &nbsp; &nbsp; <button class='cancel'>Cancel</button>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; </body></html>

米脂

这是一个在现有输入中编辑行的解决方案var counter = 0;var current_row = null;$("#btnAdd").on('click', function() {&nbsp; if (current_row == null) {&nbsp; if ( $("#insert-surname").val().length && $("#insert-name").val().length ) {&nbsp; &nbsp; let row = '<tr data-row="'+counter+'"> <td>' + $("#insert-name").val() + '</td> <td>' + $("#insert-surname").val() + '</td> <td>' + "edit" + '</td> </tr>'&nbsp; &nbsp; $('tbody').append(row);&nbsp; &nbsp; counter++;&nbsp; &nbsp; }&nbsp; } else {&nbsp; &nbsp; var select_row = $('tr[data-row='+current_row+']');&nbsp; &nbsp; let cells = $(select_row).find('td');&nbsp; &nbsp; $(cells[0]).text($("#insert-name").val());&nbsp; &nbsp; $(cells[1]).text($("#insert-surname").val());&nbsp; }&nbsp; clear_input();$('td:contains("edit")').html("<button type='button'>" + "edit" + "</button>").on('click', function() {&nbsp; &nbsp; let cells = $(this).parents('tr').find('td');&nbsp; &nbsp; $("#insert-name").val($(cells[0]).text());&nbsp; &nbsp; $("#insert-surname").val($(cells[1]).text());&nbsp; &nbsp; current_row = $(this).parents('tr').data('row');&nbsp; });});$('#btnCancel').on("click", () => clear_input());function clear_input() {&nbsp; &nbsp; current_row = null;&nbsp; &nbsp; $("#insert-name").val('');&nbsp; &nbsp; $("#insert-surname").val('');}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form>&nbsp; <div>&nbsp; &nbsp; <label for="insert-name">Name:</label>&nbsp; &nbsp; <input type="text" id="insert-name">&nbsp; </div>&nbsp; <div>&nbsp; &nbsp; <label for="insert-surname">Surname:</label>&nbsp; &nbsp; <input type="text" id="insert-surname">&nbsp; </div></form><button type="button" id="btnAdd">Add to Table</button><button type="button" id="btnCancel">Cancel</button><table id='data-table'>&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th scope="col">Name</th>&nbsp; &nbsp; &nbsp; <th scope="col">Surname</th>&nbsp; &nbsp; &nbsp; <th scope="col">Edit</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody id="tbody"></tbody></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5