我想要得到一个确认消息点击删除(这可能是一个按钮或图像)。如果用户选择“Ok”,那么删除就完成了,否则如果点击“Cancel”,什么都不会发生。
当点击按钮时,我尝试回显这个,但是回显的东西使我的输入框和文本框失去了它们的样式和设计。
我想要得到一个确认消息点击删除(这可能是一个按钮或图像)。如果用户选择“Ok”,那么删除就完成了,否则如果点击“Cancel”,什么都不会发生。
当点击按钮时,我尝试回显这个,但是回显的东西使我的输入框和文本框失去了它们的样式和设计。
当前回答
使用jQuery:
$(".delete-link").on("click", null, function(){
return confirm("Are you sure?");
});
其他回答
我知道这是老问题了,但我需要一个答案,这些答案都不是,但alpesh的答案对我有用,我想与可能有同样问题的人分享。
<script>
function confirmDelete(url) {
if (confirm("Are you sure you want to delete this?")) {
window.open(url);
} else {
false;
}
}
</script>
普通版:
<input type="button" name="delete" value="Delete" onClick="confirmDelete('delete.php?id=123&title=Hello')">
我的PHP版本:
$deleteUrl = "delete.php?id=" .$id. "&title=" .$title;
echo "<input type=\"button\" name=\"delete\" value=\"Delete\" onClick=\"confirmDelete('" .$deleteUrl. "')\"/>";
这可能不是公开的正确方式,但在一个私人网站上,这对我来说是有效的。:)
Angularjs与Javascript删除示例
html代码
<button ng-click="ConfirmDelete(single_play.play_id)" type="submit" name="actiondelete" value="1"><img src="images/remove.png" alt="Delete"></button>
“single_play。Play_id "是任意angularjs变量,假设你想在删除操作期间传递任何参数
app模块内的Angularjs代码
$scope.ConfirmDelete = function(yy)
{
var x = confirm("Are you sure you want to delete?");
if (x) {
// Action for press ok
$http({
method : 'POST',
url : 'sample.php',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({ delete_play_id : yy})
}).then(function (response) {
$scope.message = response.data;
});
}
else {
//Action for cancel
return false;
}
}
这就是使用不引人注目的JavaScript和在HTML中保存确认消息的方式。
<a href="/delete" class="delete" data-confirm="Are you sure to delete this item?">Delete</a>
这是一个纯香草JS,兼容IE 9+:
var deleteLinks = document.querySelectorAll('.delete');
for (var i = 0; i < deleteLinks.length; i++) {
deleteLinks[i].addEventListener('click', function(event) {
event.preventDefault();
var choice = confirm(this.getAttribute('data-confirm'));
if (choice) {
window.location.href = this.getAttribute('href');
}
});
}
看到它的行动:http://codepen.io/anon/pen/NqdKZq
当你在PHP和mysql中删除一些东西时,设置一个确认消息…
使用以下脚本代码:
<script>
function Conform_Delete()
{
return conform("Are You Sure Want to Delete?");
}
</script>
使用下面的HTML代码:
<a onclick="return Conform_Delete()" href="#">delete</a>
您可以更好地使用以下方法
<a href="url_to_delete" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>