douangoo48863 2016-09-30 14:06
浏览 80
已采纳

Button和ContentEditable函数

I have a table that includes both a "Edit/Save" and "Delete" button. Whenever I click the "Edit" button, it makes the rows editable and changes to a "Save" button so I can save changes. However, when I do this, it makes every cell editable and only the first row does the edit button work. It does not work for the second row, third row, etc.

My question is a two parter...

  1. How can I make certain cells in the row not editable and the others editable? I am specifically wanting the first cell "MR_ID" to not be editable.

  2. How can I get the Edit function working for multiple rows instead of only the first row?

Relevant HTML/PHP code:

<?php
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr>
        <td id="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td id="mr_name" contenteditable="false"><?php echo $rows['MR_Name']?></td>
        <td id="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
        <td id="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>     
        <td id="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
        <td id="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
        <td><button id="edit" name="edit">Edit</button>
        <button id="delRow" name="delete" onclick="deleteRow(this)">Delete</button></td>
    </tr>

Relevant Javascript code:

  $(document).ready(function() {
    $('#edit').click(function() {
        var $this = $(this);
        var tds = $this.closest('tr').find('td').filter(function() {
            return $(this).find('#edit').length === 0;
        });
        if ($this.html() === 'Edit') {
            $this.html('Save');
            tds.prop('contenteditable', true);
        } else {
            $this.html('Edit');
            tds.prop('contenteditable', false);
        }
    });
    });
  • 写回答

2条回答 默认 最新

  • duandun2136 2016-09-30 14:08
    关注

    Have a look at the jquery .not function to remove specific elements from your selection.

    Your function is only working for the first row because you are using IDs on the tds. You should only have one item with a specific id on a page. Change these to classes and your code should work. You are also specifically targeting the closest row and so will only get the first.

    Once you have fixed up these things your code should look something like:

    var tds = $this.find('tr td').not('.mr_id').filter ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容