【问题标题】:jquery delete table rowjquery删除表行
【发布时间】:2013-03-24 21:17:53
【问题描述】:

我有一张桌子

<table id="favoriteFoodTable">
    <th>
        Food Name:
    </th>
    <th>
        Restaurant Name:
    </th>
    <th>

    </th>
    <?php while ($row = $foods->fetch()) {
        ?>
        <tr>
            <td>
                <?php echo $row['foodName']; ?>
            </td>
            <td>
                <?php echo $row['restaurantName']; ?>
            </td>
            <td>
                <a class="deleteLink" href="" >delete</a>
            </td>
        </tr>
    <?php } ?>
</table>

我使用这个 jquery 函数,所以当用户点击删除时,行的背景会改变,然后行会被删除

$(document).ready(function() {
    $("#favoriteFoodTable .deleteLink").on("click",function() {
        var td = $(this).parent();
        var tr = td.parent();
        //change the background color to red before removing
        tr.css("background-color","#FF3700");

        tr.fadeOut(400, function(){
            tr.remove();
        });
    });
});

只是背景在改变,但行没有被删除,为什么?如何解决?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    该行已被删除,但由于单击使您跟随链接,因此在刷新页面时会立即恢复。

    在回调末尾添加return false;event.preventDefault();以防止默认行为:

    $(document).ready(function() {
        $("#favoriteFoodTable .deleteLink").on("click",function() {
            var tr = $(this).closest('tr');
            tr.css("background-color","#FF3700");
            tr.fadeOut(400, function(){
                tr.remove();
            });
            return false;
        });
    });
    

    Demonstration

    请注意,我使用closest 以获得更可靠的代码:如果中间有另一个元素,仍然会找到tr

    【讨论】:

    • 什么意思?点击“删除”:行淡出然后被删除。
    【解决方案2】:

    您忘记做的是在您的链接中设置哈希。 示例:

    <a class="deleteLink" href="" >delete</a>
    

    应该是

    <a class="deleteLink" href="#" >delete</a>
    

    return false;
    

    在你的结尾

    $(document).ready(function() {
        $("#favoriteFoodTable .deleteLink").on("click",function() {
            ...
            return false;
        });
    });
    

    【讨论】:

      【解决方案3】:

      这是另一种选择。

      function DeleteRowOfProductTable(productID){
          $('#YourTableId tr').each(function (i, row) {
              var $row = $(row);
              var productLabelId =  $row.find('label[name*="Product_' + productID + '"]');
      
             var $productLabelIdValue = $productLabelId.text();
             if (parseInt(productID) == parseInt($productLabelIdValue))
             {
                 $row.remove(); 
             }
          });
       }
      

      【讨论】:

        【解决方案4】:

        如果您希望一次只选择表格中的 1 行

        $("#data tr").click(function() {
            var selected = $(this).hasClass("highlight");
            $("#data tr").removeClass("highlight");
            if(!selected)
                    $(this).addClass("highlight");
        

        【讨论】:

          【解决方案5】:

          试试

          var tr = $(this).closest('tr');
          tr.detach();
          

          为我工作!

          【讨论】:

          • 请在您的回答中添加更多描述
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-04
          • 2013-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多