0

假设我有一张桌子:

<table class="table table-bordered table-hover" id="myTableJD">
    <thead>
        <tr>
            <th>JOB DESCRIPTION</th>
            <th>CONTROL</th>
        </tr>
    </thead>
    <tbody>                     

        <?php
            foreach ($JobDescription as $rowJD1)
                {
                    echo '<tr id="itemk_'.$rowJD1["id"].'">';
                    echo '<td class="customerIDCell">'.$rowJD1["JobDescription"].'</td>';
                    echo '<td><a href="#" class="edit_buttonk" id="editk-'.$rowJD1["id"].'">Edit</a> | <a href="#" class="del_buttonk" id="delk-'.$rowJD1["id"].'">Delete</a></td>';
                    echo '</tr>'; //i changed this part to </tr> and close the last </td> to avoid confusion
                }
?>
</tbody>
</table>

这是我使用 jquery jquery-1.8.2.min.js的 javascript

    //##### Add record when Add Record Button is clicked #########
    $("#FormSubmitJD").click(function (e) {

        e.preventDefault();

        if($("#contentTextJD").val()==="") //simple validation
        {
            alert("Please enter some text!");
            return false;
        }

        var myData = "jd="+ $("#contentTextJD").val(); //post variables


        jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "<?php echo BASE_URL."programs/SaveJobDescription.php"; ?>", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //post variables
            success:function(response){
            $('#IDJOBDESCRIPTION').modal('hide');   
             <?php 
             $getLastID = $emp->getLastID($_SESSION['UEmpID']) + 1;
             $itemk_id = "itemk_".$getLastID; 
             $delk_id = "delk-".$getLastID; 
             $editk_id = "editk-".$getLastID; 
             ?>
             $('#myTableJD').append('<tr id="<?php echo $itemk_id; ?>"><td>'+$("#contentTextJD").val()+'</td><td><a href="#" class="edit_buttonk" id="<?php echo $editk_id; ?>" >Edit</a> | <a href="#" class="del_buttonk" id="<?php echo $delk_id; ?>">Delete</a></td></tr>');
            $("#contentTextJD").val(''); //empty text field after successful submission
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError); //throw any errors
            }
        });
    });
    //##### edit record when edit Button is clicked #########
   $("body").on("click", ".edit_buttonk", function(e) {
        e.preventDefault();
        var clickedID = this.id.split("-"); //Split string (Split works as PHP explode)
        var DbNumberID = clickedID[1]; //and get number from array
             <?php 
             $getJD = $emp->getJD($_SESSION['UEmpID']);
             $jdValue = $getJD; 
             ?>
        var tr = $(this).parent().parent();
        alert(tr);
        var new_row = '<tr id="itemk_'+DbNumberID+'"><td style="margin-bottom:-10px;padding-bottom:0px;"><input id="jd-'+DbNumberID+'" type="text" value="<?php echo $jdValue; ?>" class="span12"/> </td><td><a href="#" class="save_buttonk" id="jdSave-'+DbNumberID+'">Save</a></td></tr>';
        alert(new_row);
        tr.replaceWith(new_row);        
    }); 
    //##### save record when save Button is clicked #########
   $("body").on("click", ".save_buttonk", function(e) {
        e.preventDefault();
        var clickedID = this.id.split("-"); //Split string (Split works as PHP explode)
        var DbNumberID = clickedID[1]; //and get number from array
        var myData = 'id='+DbNumberID+'&jd='+$("#jd-"+DbNumberID).val(); //build a post data structure
        alert(myData);
        jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "<?php echo BASE_URL."programs/EditJobDescription.php"; ?>", //Where to make Ajax calls
            data:myData, //post variables
            success:function(response){
            //on success, hide element user wants to delete.
            <?php 
             $getJD = $emp->getJD($_SESSION['UEmpID']);
             $jdValue = $getJD; 

             ?>
            var tr = $(this).parent().parent();
                var new_row = '<tr id="<?php echo $itemk_id; ?>"><td><?php echo $jdValue; ?></td><td><a href="#" class="edit_buttonk" id="<?php echo $editk_id; ?>" >Edit</a> | <a href="#" class="del_buttonk" id="<?php echo $delk_id; ?>">Delete</a></td></tr>';
        tr.replaceWith(new_row);


             $('#<?php echo $itemk_id; ?>').empty();
            $('#<?php echo $itemk_id; ?>').append(new_row);
            $('#<?php echo $itemk_id; ?>').replaceWith(new_row);            
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
        });
    }); 
    //##### Delete record when delete Button is clicked #########
   $("body").on("click", ".del_buttonk", function(e) {
        e.preventDefault();
        var clickedID = this.id.split("-"); //Split string (Split works as PHP explode)
        var DbNumberID = clickedID[1]; //and get number from array
        var myData = 'id='+ DbNumberID; //build a post data structure
        alert(myData);
        jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "<?php echo BASE_URL."programs/DeleteJobDescription.php"; ?>", //Where to make Ajax calls
            data:myData, //post variables
            success:function(response){
            //on success, hide element user wants to delete.
            $('#itemk_'+DbNumberID).fadeOut("slow");
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
        });
    });

一切正常,除了通过单击编辑链接替换表的特定行之后,当我单击保存编辑的字段时没有任何反应。我想显示表格的原始显示但已编辑。

有人有什么想法吗?

4

1 回答 1

0

//Data added on table when click on submit.

var edit_row = null;

$("#submit").on("click", function () {
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var number = $("#number").val();
    var row = "<tr><td class='first'>" +
        fname + 
        "</td><td class='last'>" +
        lname +
        "</td><td class='mobile'>" +
        number +
        '</td><td><a href="" class="fas fa-edit edit text-primary" style="cursor:pointer"></a> <a class="fas fa-trash-alt dlt text-danger" style="cursor:pointer"></a></td></tr>';

    if (edit_row) {
        $("#myTable tbody").find($(edit_row)).replaceWith(row);
        edit_row = null;
    }
    else if ((fname != "" && lname != "" && number != "")) {
        $("#myTable tbody").append(row);
    }

// After submit all values are null using below codes.

    $("#fname").val('');
    $("#lname").val('');
    $("#number").val('');
});

// Data delete in table when click on delete button.

$(document).on('click', '.dlt', function () {
    $(this).parents('tr').remove();
    return false;
});

// Data again fill in form when click on edit.

$(document).on('click', '.edit', function () {
    edit_row = $(this).closest('tr');
    $("#fname").val($(this).closest('tr').find('.first').text());
    $("#lname").val($(this).closest('tr').find('.last').text());
    $("#number").val($(this).closest('tr').find('.mobile').text());
});


// Thank you.
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image"
        href="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQoqksPBDeWpznZfj0_XtgkNO5npIUwP7n7n0GCXos3PGoEGHLwapvcf_qlnbXWf-bd-Us&usqp=CAU">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css">
    <style>
        .form-label {
            margin: 0%;
        }
    </style>

    <title>Edit and Delete</title>
</head>

<body>

    <!-- form -->

    <div class="container mt-5 d-flex justify-content-center">
        <form class="row" id="myForm">
            <div class="col-8">
                <label for="fname" class="form-label">First Name</label>
                <input type="text" class="form-control" id="fname" placeholder="First Name">
            </div>
            <div class="col-8 mt-3">
                <label for="lname" class="form-label">Last Name</label>
                <input type="text" class="form-control" id="lname" placeholder="Last Name">
            </div>
            <div class="col-8 mt-3">
                <label for="number" class="form-label">Mobile Number</label>
                <input type="tel" class="form-control" maxlength="10" minlength="10" id="number"
                    placeholder="Mobile Number">
            </div>
            <div class="col-12 mt-3">
                <button type="button" id="submit" class="btn btn-primary">Submit</button>
            </div>
        </form>
    </div>

    <!-- Table -->

    <table class="table container mt-5" id="myTable">
        <thead>
            <tr>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Mobile Number</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <footer style="float: right;display: block;position: fixed;bottom: 20px;right: 20px;">Made by <a style="text-decoration: none;color: black;font-weight: 600;" href="https://www.instagram.com/_ridham_golakiya/">Ridham Golakiya</a></footer>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/js/all.min.js"></script>
    <script src="edit&delete.js"></script>
</body>

</html>

于 2021-11-12T07:13:15.793 回答