最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting element Id Sweet Alert - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use sweet alert as a dialog to confirm the deletion of an entry on a table. There's a column on each table row which has a button which opens a 'Sweet Alert' confirm dialog.

The code of my table is as follow:

<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>

The code for my sweet alert dialog is:

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"); });

When the user clicks yes on the dialog, I want to get the id of the button which the user just clicked. That way I'll be able to get the value of the attribute userid which I can then use to delete the entry from database.

I'm trying to use sweet alert as a dialog to confirm the deletion of an entry on a table. There's a column on each table row which has a button which opens a 'Sweet Alert' confirm dialog.

The code of my table is as follow:

<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>

The code for my sweet alert dialog is:

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"); });

When the user clicks yes on the dialog, I want to get the id of the button which the user just clicked. That way I'll be able to get the value of the attribute userid which I can then use to delete the entry from database.

Share Improve this question asked Feb 21, 2016 at 11:55 Melvin M.Melvin M. 3,5003 gold badges16 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can get the id of the clicked button by retrieving it when the button is clicked, and passing it as a parameter to the plugin callback.
Take a look at the following implementation:

$('#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/jwsho9L9/4/

发布评论

评论列表(0)

  1. 暂无评论