jquery + table row edit - String problem

孤人 提交于 2019-12-23 04:17:07

问题


I've one problem with editing a row... I wanted to use a button to enter edit mode in the table (generated from php array (data taken from mysql))

I have two rows for each data:

                <tr class="dataline">
                    <td><?=$row->id; ?></td>
                    <td><?=$row->pl>0 ? '<div id="gain">' . $row->pl .'</div>' : '<div id="loss">' . $row->pl . '</div>';?></td>
                    <td><div id="reason"><?=$row->reason;?></div></td>
                    <td><div id="comment"><?=$row->comment;?></div></td>
                    <td><div id="date"><?=$row->cdate; ?><br /><?=$row->ctime; ?></div></td>
                    <td><div id="date"><?=$row->mdate; ?><br /><?=$row->mtime; ?></div></td>
                    <td colspan="2"><button id="editlink">Edit</button>
                    <button id="deletelink">Delete</button></td>
                </tr>
                <tr class="editline" style="display:none;">
                    <form id="<?php echo $row->id;?>">
                    <td><?php echo $row->id; ?><input type="hidden" name="id" id="id" value="<?php echo $row->id;?>" /></td>
                    <td><input type="text" id="pl" name="pl" value="<?=$row->pl;?>" /></td>
                    <td><textarea id="reason" name="reason"><?=$row->reason;?></textarea></td>
                    <td><textarea id="comment" name="comment"><?=$row->comment;?></textarea></td>
                    <td><div id="date"><?=$row->cdate; ?><br /><?=$row->ctime; ?></div></td>
                    <td><div id="date"><?=$row->mdate; ?><br /><?=$row->mtime; ?></div></td>
                    <td colspan="2"><input id="edit_save" type="Submit" value="Save" />
                    </form>
                    <button id="cancellink">Cancel</button></td>
                </tr>

I attached two jquery statements to it ...

1st. one ... changes the row to

    $("#editlink").click(function() {
        var datapos = $(this).parent().parent().prevAll().length;
        var editpos = datapos + 1;

        $("#data_table tbody tr:eq(" + datapos + ")").hide();
        $("#data_table tbody tr:eq(" + editpos + ")").show();
    });

Works perfectly.

2nd. Suppose to save (POST to PHP script) once the change has been done and reload the page.

    $("#edit_save").click(function() { 
        var dataString = $("form").serialize();

        var editpos = $(this).parent().parent().prevAll().length;
        var datapos = editpos - 1;

        $.ajax({
            type: "POST",
            url: "edit",
            data: dataString,
            success: function() {
                $("#lightbox").fadeIn(900);
                $("#notification-box").show();
                $("#notification-box").html("<img src='<?php base_url();?>img/notification.gif'><p>Saving</p>");

            location.reload();
            }
        });
    });

So, the issue I have here is that the dataString is a value of all values generated in the table not the specific row I wanted to edit.

I would be really glad if someone can help me with that.

Cheers,

/Jacek


回答1:


$("form") would fetch all <form> elements in the page, you need the clicked button parent form: $(this).parent('form'):

var dataString = $(this).parent('form').serialize();


来源:https://stackoverflow.com/questions/3642310/jquery-table-row-edit-string-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!