获取元素 ID 甜蜜警报



我正在尝试使用甜蜜警报作为对话框来确认删除表上的条目。每个表格行上都有一个列,其中有一个按钮,可以打开"甜蜜警报"确认对话框。

我的表的代码如下:

<tr>
    <!-- Other Table Colums -->
    <td>
    <button userid="<?php echo $row['id']; ?>" id="<?php echo('bn_delete_' . $row['id']); ?>" class="btn btn-danger btn-xs warning-message-parameter">Delete</button>
    <td>
 </tr>

我的甜蜜警报对话框的代码是:

swal({   title: "Are you sure?",   
         text: "You will not be able to recover this imaginary file!", 
         type: "warning",   showCancelButton: true,   confirmButtonColor: "#DD6B55",   
         confirmButtonText: "Yes, delete it!",   
         closeOnConfirm: false }, 
         function(){   
               swal("Deleted!", "Your imaginary file has been deleted.", "success"); });

当用户在对话框中单击"是"时,我想获取用户刚刚单击的按钮的 ID。这样,我将能够获取属性userid的值,然后可以使用该值从数据库中删除条目。

您可以通过在单击按钮时检索它并将其作为参数传递给插件回调来获取单击按钮的 id。
看看下面的实现:

$('#openSwal').on('click', function(e) {
  var id = $(e.currentTarget).attr("id"); 
  var userId = $(e.currentTarget).data("user-id"); 
  var region = "myregion";
  swal({
    html: true,
    title: '' + region,
    showCancelButton: true,
    showConfirmButton: true,
    confirmButtonText: "save", 
    text: "<span onclick='save()'>l</span>"
  }, function() { 
    save(id, userId);
  });
});
function save(id, userId) {
  console.log(id);
  console.log(userId); 
}

http://jsfiddle.net/jwsho9L9/4/

最新更新