【问题标题】:Buttons in table row are not working after adding a row click function添加行点击功能后表格行中的按钮不起作用
【发布时间】:2020-09-16 17:18:17
【问题描述】:

我正在使用 bootstrap 4 并获得了一个表格,其中表格行是可点击的。每行还有两个按钮。但是按钮不再起作用,因为它正在执行单击行所做的任何事情。

这是一个示例表

    <table id="subjects" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th class="d-none">IDs</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <tr data-href="subjects/blah">
                <td nowrap class="d-none">
                    <div>121212</div>
                </td>
                <td>
                    <button type="button" class="btn btn-dark" onclick="location.href='subject/fff'">Edit</button>
                    <button type="button" class="btn btn-dark" data-toggle="modal" data-target="#confirm-delete" data-href="subject/delete/blah">Delete</button>
                </td>
            </tr>
        </tbody>
    </table>

    <script>
        $(document).ready(function() {


            $('.table tr[data-href]').each(function(){
                $(this).css('cursor','pointer').hover(
                    function(){ 
                        $(this).addClass('active'); 
                    },  
                    function(){ 
                        $(this).removeClass('active'); 
                    }).click( function(){ 
                        document.location = $(this).attr('data-href'); 
                    }
                );
            });
        });
    </script>

第一个按钮有一个 onclick,第二个按钮打开一个引导模式对话框。

目前,在我删除表格行 jquery 单击功能之前,这些按钮都不起作用。我怎样才能让它很好地工作?

【问题讨论】:

标签: jquery asp.net-mvc bootstrap-4


【解决方案1】:
$('.table td.row-link').each(function(){
  $(this).css('cursor','pointer').hover(
      function(){ 
          $(this).parent().addClass('active'); 
      },  
      function(){ 
          $(this).parent().removeClass('active'); 
      }).click( function(){ 
          document.location = $(this).parent().attr('data-href'); 
      }
  );
});

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 2019-09-09
    • 2021-02-08
    • 2019-01-20
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多