问题
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.
回答1:
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.net/jwsho9L9/4/
来源:https://stackoverflow.com/questions/35535774/getting-element-id-sweet-alert