【问题标题】:Make All Columns in HTML Table Editable Except One使 HTML 表格中的所有列都可编辑,除了一个
【发布时间】:2017-04-16 09:10:20
【问题描述】:

我有一个带有编辑按钮的 HTML 表格。单击编辑按钮时,它允许该行变为可编辑。现在整行都是可编辑的,但是,我希望 MR_ID 行不可编辑。我怎样才能做到这一点?我在我的代码中尝试了一些东西,但它似乎不起作用。

如果我需要提供更多代码,请告诉我,我会提供的。

这是我的 if 语句的开头,我认为问题应该是:

  $(document).on("click", "#html_master .edit", function () {
  var $this = $(this);
  var tds = $this.closest('tr').find('td').filter(function () {
    return $(this).find('.edit').length === 0;
  });
  if ($this.val() === 'Edit') {
    $this.val('Save');
   if($this.id != '.mr_id'){
        tds.prop('contenteditable', true);
   }
}

表格的 HTML/PHP:

<?php
/* Foreach loop that brings in information to populate table */
foreach ($dbh->query($sql) as $rows){
    ?>
    <tr id="<?php echo intval ($rows['MR_ID'])?>">
        <td class="mr_id" id="<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td class="mr_name" id="mr_name-<?php echo intval ($rows['MR_ID'])?>" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td>
        <td class="buyer_id" id="buy_id<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
        <td class="poc_n" id="poc_n-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>     
        <td class="poc_e" id="poc_e-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
        <td class="poc_p" id="poc_p-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
        <td><input type="button" class="edit" name="edit" value="Edit">
    </tr>
 <?php
  }
 ?>

【问题讨论】:

  • 什么是$this。如果它是一个 jquery 对象,那么你不能通过 .id 获取 id 看起来你也可以与一个类进行比较,它不应该只是 'mr_id' (没有前导点)
  • 我在 javascript 中添加了一些额外的代码来帮助您了解 $this 是什么
  • 当您单击编辑时,您是否希望每个tdmr_id 都可编辑?
  • 是的,没错

标签: javascript php html html-table contenteditable


【解决方案1】:

您可以将其直接放入您的初始查找语句中。

var tds = $this.closest('tr').find('td').not('.mr_id').filter(function () {
  return $(this).find('.edit').length === 0;
})

另一个选项,如果编辑单元格总是最后一个,则将其捆绑到 not 语句中

var tds = $this.closest('tr').find('td').not('.mr_id, :last-child')

【讨论】:

    【解决方案2】:

    只需扩展您的过滤器以不包括 mr_id 列:

    $(document).on("click", "#html_master .edit", function() {
      var $this = $(this);
    
      var tds = $this.closest('tr').children().filter(function() {
        var $thisTd = $(this);
        return $thisTd.find('.edit').length === 0 && !$thisTd.hasClass('mr_id');
      });
    
      if ($this.val() === 'Edit') {
        $this.val('Save');
        tds.prop('contenteditable', true);
      }
    });
    

    【讨论】:

    • 如果您已经在使用 jQuery,则无需执行所有这些操作。一个简单的.not 干净得多
    • @jmcgriz,他已经在使用过滤器 - 不妨扩展它 - 你的 tds 也包括 .edit 列
    • 我只是在他已有的基础上进行构建,而不是替换它。为了清楚起见,我将进行编辑
    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多