如何在可添加,编辑和删除的动态表上保留复选框值?

时间:2018-08-16 19:07:18

标签: javascript jquery html

我想允许用户编辑他们输入表中的值,但是有一个复选框,当它处于不可编辑状态时,我想禁用它,并在用户输入它时保留其选中或未选中状态,或者编辑它。

当前,即使未对其进行编辑并且不保留其选中状态,也可以选中该复选框。

下面的代码就是我对此的理解。

$(function () {
    //Add, Save, Edit and Delete functions code
    $(".btnEdit").on("click", Edit);
    $(".btnDelete").on("click", Delete);
    $("#btnAdd").on("click", Add);
});

function Add() {

    $("#tblData tbody").append(
        "<tr>" +
        "<td><input type='text' /></td>" +
        "<td><input type='text' /></td>" +
        "<td><input type='checkbox' /></td>" +
        "<td><span class='btnSave'> <a href='#' class='btn btn-link'>Save</a></span>|<span class='btnDelete'> <a href='#' class='btn btn-link'>Delete</a></span></td>" +
        "</tr>"
    );

    $(".btnSave").on("click", Save);
    $(".btnDelete").on("click", Delete);
};

function Save() {
    var par = $(this).parent().parent(); //tr

    var tdName = par.children("td:nth-child(1)");
    var tdPhone = par.children("td:nth-child(2)");
    var tdEmail = par.children("td:nth-child(3)");
    var tdButtons = par.children("td:nth-child(4)");

    tdName.html(tdName.children("input[type=text]").val());
    tdPhone.html(tdPhone.children("input[type=text]").val());
    tdEmail.html(tdEmail.children("input[type=checkbox]").val());
    tdButtons.html("<span class='btnEdit'> <a href='#' class='btn btn-link'>Edit</a></span><span class='btnDelete'> <a href='#' class='btn btn-link'>Delete</a></span>");

    $(".btnEdit").on("click", Edit);
    $(".btnDelete").on("click", Delete);
};

function Edit() {
    var par = $(this).parent().parent(); //tr
    var tdName = par.children("td:nth-child(1)");
    var tdPhone = par.children("td:nth-child(2)");
    var tdEmail = par.children("td:nth-child(3)");
    var tdButtons = par.children("td:nth-child(4)");

    tdName.html("<input type='text' id='txtName' value='" + tdName.html() + "'/>");
    tdPhone.html("<input type='text' id='txtPhone' value='" + tdPhone.html() + "'/>");
    tdEmail.html("<input type='checkbox' id='txtEmail' value='" + tdEmail.html() + "'/>");
    tdButtons.html("</td><span class='btnSave'> <a href='#' class='btn btn-link'>Save</a></span></td>");

    $(".btnSave").on("click", Save);
    $(".btnEdit").on("click", Edit);
    $(".btnDelete").on("click", Delete);
};

function Delete() {
    var par = $(this).parent().parent(); //tr
    par.remove();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnAdd">New</button>
<table id="tblData">
    <thead>
        <tr>
            <th>Name</th>
            <th>Phone</th>
            <th>Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

1 个答案:

答案 0 :(得分:3)

您可以通过选中checked attribute保留复选框的状态。

如果选中的属性存在,则该复选框将被选中,并且在未选中的情况下它将为空白。


在保存功能中

更改此

tdEmail.html(tdEmail.children("input[type=checkbox]").val());

为此

 var email=(tdEmail.children("input[type=checkbox]").prop('checked')==true)?"checked":"";
 tdEmail.html("<input type='checkbox' disabled  id='txtEmail'" + email + "/>");


在“编辑”功能中

更改此

tdEmail.html("<input type='checkbox' id='txtEmail' value='" + tdEmail.html() + "'/>");

对此

var email=(tdEmail.children("input[type=checkbox]").prop('checked')==true)?"checked":""; 
tdEmail.html("<input type='checkbox' id='txtEmail'" + email + "/>");

$(function () {
            //Add, Save, Edit and Delete functions code
            $(".btnEdit").on("click", Edit);
            $(".btnDelete").on("click", Delete);
            $("#btnAdd").on("click", Add);
        });

        function Add() {

            $("#tblData tbody").append(
                "<tr>" +
                "<td><input type='text' /></td>" +
                "<td><input type='text' /></td>" +
                "<td><input type='checkbox' /></td>" +
                "<td><span class='btnSave'> <a href='#' class='btn btn-link'>Save</a></span>|<span class='btnDelete'> <a href='#' class='btn btn-link'>Delete</a></span></td>" +
                "</tr>");

            $(".btnSave").on("click", Save);
            $(".btnDelete").on("click", Delete);
        };

        function Save() {
            var par = $(this).parent().parent(); //tr

            var tdName = par.children("td:nth-child(1)");
            var tdPhone = par.children("td:nth-child(2)");
            var tdEmail = par.children("td:nth-child(3)");
            var tdButtons = par.children("td:nth-child(4)");
            
     
     tdName.html(tdName.children("input[type=text]").val());
            tdPhone.html(tdPhone.children("input[type=text]").val());
        var emailCheck= tdEmail.children("input[type=checkbox]").prop('checked');
        //console.log(emailCheck);
        var email=(emailCheck==true)?"checked":"";
         //console.log(email);
         tdEmail.html("<input type='checkbox' disabled  id='txtEmail'" + email + "/>");
        
            tdButtons.html("<span class='btnEdit'> <a href='#' class='btn btn-link'>Edit</a></span><span class='btnDelete'> <a href='#' class='btn btn-link'>Delete</a></span>");

            $(".btnEdit").on("click", Edit);
            $(".btnDelete").on("click", Delete);
            
             
        };

        function Edit() {
            var par = $(this).parent().parent(); //tr
            var tdName = par.children("td:nth-child(1)");
            var tdPhone = par.children("td:nth-child(2)");
            var tdEmail = par.children("td:nth-child(3)");
            var tdButtons = par.children("td:nth-child(4)");
                  var emailCheck= tdEmail.children("input[type=checkbox]").prop('checked');
        //console.log(emailCheck);
        var email=(emailCheck==true)?"checked":""; 
            tdName.html("<input type='text' id='txtName' value='" + tdName.html() + "'/>");
            tdPhone.html("<input type='text' id='txtPhone' value='" + tdPhone.html() + "'/>");
            tdEmail.html("<input type='checkbox' id='txtEmail'" + email + "/>");
            tdButtons.html("</td><span class='btnSave'> <a href='#' class='btn btn-link'>Save</a></span></td>");

            $(".btnSave").on("click", Save);
            $(".btnEdit").on("click", Edit);
            $(".btnDelete").on("click", Delete);
        };

        function Delete() {
            var par = $(this).parent().parent(); //tr
            par.remove();
        };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnAdd">New</button>
<table id="tblData">
    <thead>
        <tr>
            <th>Name</th>
            <th>Phone</th>
            <th>Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

注意:也许还有更多更好的方法来编写代码,但在这里我指出了解决问题的方法。