带有自定义按钮的 JavaScript 确认框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript confirm box with custom buttons
提问by Hector Barbossa
Can I write a custom confirm box in JavaScript that, instead of the default OK
and CANCEL
button, shows a SAVE
and DELETE
button?
我可以在 JavaScript 中编写一个自定义确认框,而不是默认的OK
和CANCEL
按钮,而是显示一个SAVE
和DELETE
按钮吗?
回答by Tim Schmelter
Use the jQuery UI Dialog Boxfor this.
为此使用jQuery UI 对话框。
You can do something like this:
你可以这样做:
JS:
JS:
$(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Do it": function() {
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
<link href="https://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/>
<link href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="dialog-confirm" title="Is it treason, then?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Am I the Senate?</p>
</div>
回答by Tim Down
Not with the native JavaScript confirm box. You could use one of the many JavaScript UI components that emulate modal dialogs to do this instead.
不适用于本机 JavaScript 确认框。您可以使用模拟模态对话框的众多 JavaScript UI 组件之一来执行此操作。
Here's an example: https://jqueryui.com/dialog/#modal-confirmation
这是一个例子:https: //jqueryui.com/dialog/#modal-confirmation
I've never used this so use at your own risk.
我从未使用过这个,所以使用风险自负。
回答by Belinda
$('confirm text').dialog(
{
modal:true, //Not necessary but dims the page background
buttons:{
'Save':function() {
//Whatever code you want to run when the user clicks save goes here
},
'Delete':function() {
//Code for delete goes here
}
}
}
);
This should work, but you will need to download or link to jQuery and the jQuery UI libraries to run this.
这应该可以工作,但您需要下载或链接到 jQuery 和 jQuery UI 库才能运行它。