简体   繁体   English

如何使用 javascript/jquery 在每一行中添加编辑和删除按钮

[英]How to add Edit and delete button in each row using javascript/jquery

I want to add edit and delete button in each row and want to edit and delete row using javascript.我想在每一行中添加编辑和删除按钮,并想使用 javascript 编辑和删除行。 How should I do this.我该怎么做。 Please check the below jsfiddle.请检查下面的jsfiddle。

First Name:<br>
<input type="text" id="fName" /><br>
Last Name: <br> 
<input type="text" id="lName" /><br>
Gender: <br>
<input type="text" id="gender" /><br>
Age: <br>
<input type="text" id ="age" /> <br>
<input type="button" id ="display" value="Display" /><br>
<table id= "table" border="1">
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
</table>

javascript javascript

(function setup() {
    "use strict";

var fNameElem = document.getElementById("fName");
var lNameElem = document.getElementById("lName");
var genderElem = document.getElementById("gender");
var ageElem = document.getElementById("age");
var tableElem = document.getElementById("table");
document.getElementById("display").addEventListener("click", function () {
    var newRow = tableElem.insertRow(-1);
    var newCell = newRow.insertCell(0);
    var newText = document.createTextNode(lNameElem.value + ", " + fNameElem.value);
    newCell.appendChild(newText);
    newCell = newRow.insertCell(1);
    newText  = document.createTextNode(genderElem.value);
    newCell.appendChild(newText);
    newCell = newRow.insertCell(2);
    newText  = document.createTextNode(ageElem.value);
    newCell.appendChild(newText);
    fNameElem.value = "";
    lNameElem.value = "";
    ageElem.value = "";
    tableElem.value = "";
});
})();

http://jsfiddle.net/xnWSV/ http://jsfiddle.net/xnWSV/

Ended up using jQuery, if you click edit the fields get repopulated and then you click 'Display' again to solve the problem.最终使用 jQuery,如果您单击编辑字段会重新填充,然后您再次单击“显示”以解决问题。 This is a very simple implementation of this, but I would recommended using Angular or some kind of templating libary if you were going to expand on this.这是一个非常简单的实现,但如果您要对此进行扩展,我建议您使用 Angular 或某种模板库。

http://jsbin.com/jevejetofo/edit?html,js,console,output http://jsbin.com/jevejetofo/edit?html,js,console,output

JavaScript: JavaScript:

(function setup() {
    var selectedRow = null;
    var keys = ["fName","lName","gender","age"];

    function resetValues(fName,lName,gender,age){
        forEachInput(function(key){
            $("#"+key).val('');
        });
    }

    function forEachInput(callback){
        for(var i = 0; i < keys.length; i++) {
            callback(keys[i]);
        }
    }

    function createRow(){
        var row    = $(".tr_clone").clone()
        var newRow = row.appendTo("table").removeClass("tr_clone").fadeIn(1000);
        forEachInput(function(key){
            newRow.find("."+key).text($("#"+key).val());
        });
        resetValues();
        selectedRow = null;
    }

    function applyValues(row){
        forEachInput(function(key){
            row.find("."+key).text($("#"+key).val());
        });
    }


    function editRow(row){
        forEachInput(function(key){
            $("#"+key).val(row.find("."+key).text());
        });
        selectedRow = row;
    }

    $("#display").on("click", function () {
        if(selectedRow === null){
            createRow();
            resetValues();
        } else {
            applyValues(selectedRow);
            resetValues();
            selectedRow = null;
        }
    });

    $("table").delegate("button").on("click",function(e){
        e.preventDefault(); 
        if($(e.target).hasClass('edit')){
            editRow($(e.target).parent("td").parent("tr"));
        } else {
           $(e.target).parent("td").parent("tr").fadeOut(1000).remove();            
        }
    });

})();

HTML HTML

<script src="https://code.jquery.com/jquery-2.0.3.js"></script>First Name:<br>
<input type="text" id="fName" /><br>
Last Name: <br> 
<input type="text" id="lName" /><br>
Gender: <br>
<input type="text" id="gender" /><br>
Age: <br>
<input type="text" id ="age" /> <br>
<input type="button" id="display" value="Display" /><br>
<table id= "table" border="1">
<tr>
<th>First Name</th>
  <th>Last Name</th>
<th>Gender</th>
<th>Age</th>
</tr>

      <tr class="tr_clone" style="display:none;">
        <td class="fName"></td>
        <td class="lName"><input type="text" autofocus placeholder="location" name="location" ></td>
        <td class="gender"><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker"></td>
        <td class="age"><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker"></td>
        <td class="buttons"><button class="delete">Delete</button> | <button class="edit">Edit</button></td>
    </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript和jquery将按钮添加到每一行 - add button to each row using javascript and jquery 有什么方法可以在DHTMLX网格的每一行中添加编辑/删除按钮吗? - Is there any way to add edit/delete button in each row for DHTMLX grid? 如何使用ajax和jquery在数据表中使用for循环为每行添加按钮 - How to add button for each row using for loop in datatables by ajax and jquery 如何使用jQuery在每一行的单选按钮中添加计数? - How to add count with each row's radio button using jQuery? 如何在数据表的每一行中添加编辑/删除按钮 - how to add edit/delete buttons in each row of datatable 无法使用Jquery在模式表中添加“编辑”和“删除”按钮? - Unable to add Edit and delete button in modal table using Jquery? 如何识别特定项目 <select>并在jquery中的每一行添加一个删除按钮 - How can I identify a specific item in <select> and add a delete button each row in jquery 单击“编辑”按钮时,如何使用jquery从循环表的每一行传递id? - How to pass the id from each row of a loop table using jquery when edit button is click? 如何在Javascript中编辑和删除行? - How to edit and delete the row in Javascript? 在表格行的末尾添加编辑/删除按钮 - Add edit/delete button at the end of the table row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM