【问题标题】:Exclude child node from data-href functionality从 data-href 功能中排除子节点
【发布时间】:2022-02-01 02:09:10
【问题描述】:

我有一个表,我在每一行上使用 data-href 属性,这样我就可以访问每一行对应的页面。在每一行的末尾,我放置了一个“编辑”和一个“删除”按钮。他们工作正常,直到我添加了甜蜜警报 2 确认模式。问题是当我单击删除按钮时,父 tr 节点的 data-href 被触发(以及删除功能)。如何从 tr data-href 功能中排除此按钮(甚至整个删除表单)?

tr 节点:

<tr data-href="view_client/{{ $client->id}}">     
   <td><strong>{{$client->surname }}</strong></td>
   ...
   <td style="width:15%" >
        <div class="d-flex justify-content-evenly">
             <a href="/edit_client/{{ $client->id }}" class="btn btn-sm btn-warning flex-fill">
             <i class="far fa-edit"></i>Edit</a>
             <form action="/clients/{{ $client->id }}" id="deleteForm" method="POST">
             @method('DELETE')
             @csrf
                  <button type="button" class="btn btn-danger show_confirm"><i class="far fa-trash-alt"></i></button>
             </form>
        </div>
   </td>
</tr>

show_confirm的js:

$('.show_confirm').on('click', function(e) {
        var form =  $('#deleteForm');
        e.preventDefault();
        Swal.fire({
            title: "Delete Confirmation",
            text: "Are you sure?",
            icon: "warning",
            showCancelButton: true,
              confirmButtonColor: '#ff0f15',
              cancelButtonColor: '#ed032d9e9e9e',
              confirmButtonText: 'Yeah! Go ahead!'
        })
        .then((willDelete) => {
          if (willDelete.isConfirmed) {
            form.trigger('submit');
          }
        });
    });

【问题讨论】:

标签: html href bootstrap-5


【解决方案1】:

看起来您需要防止对子元素的点击冒泡到父元素。这可以通过致电event.stopPropagation() 来完成,请阅读此处的第二段:https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
因此,只需在您的事件处理程序中添加调用:

    $('.show_confirm').on('click', function(e) {
        var form =  $('#deleteForm');
        e.preventDefault();
        e.stopPropagation(); // <---here
        Swal.fire({
            title: "Delete Confirmation",
            text: "Are you sure?",
            icon: "warning",
            showCancelButton: true,
              confirmButtonColor: '#ff0f15',
              cancelButtonColor: '#ed032d9e9e9e',
              confirmButtonText: 'Yeah! Go ahead!'
        })
        .then((willDelete) => {
          if (willDelete.isConfirmed) {
            form.trigger('submit');
          }
        });
    });

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多