Javascript 在 jQuery 确认框上添加 Yes 和 No 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8849269/
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
Add Yes and No buttons on jQuery confirmation box
提问by Sachin J
I want "Yes" and "No" buttons on a confirmation box without using jQuery UI Dialog.
我想要在不使用 jQuery UI 对话框的情况下在确认框中显示“是”和“否”按钮。
Is there a way to do this?
有没有办法做到这一点?
回答by Quasdunk
The jQuery UI Dialogis a very flexible and powerful tool for that. Here's an example of how to ask the user if he wants to delete something:
在jQuery UI的对话框是一个非常灵活和强大的工具。这是一个如何询问用户是否要删除某些内容的示例:
<div id="dialog_box" style="display: none;">
Really delete?
</div>
<button class="delete_btn">Delete</button>
<script>
$('.delete_btn').click(function(){
$('#dialog_box').dialog({
title: 'Really delete this stuff?',
width: 500,
height: 200,
modal: true,
resizable: false,
draggable: false,
buttons: [{
text: 'Yep, delete it!',
click: function(){
//delete it
}
},
{
text: 'Nope, my bad!',
click: function() {
$(this).dialog('close');
}
}]
});
});
</script>
//EDIT:
//编辑:
Sorry, it was not quite clear from your original question that you do not want to use the jQuery Dialog (but why tag the question with jquery/jquery-ui then?).
抱歉,从您最初的问题中并不清楚您不想使用 jQuery 对话框(但是为什么用 jquery/jquery-ui 标记问题呢?)。
AFAIK, there is no other way to accomplish what you want to do. Also, keep in mind, that the text of the native JavaScript dialog box also takes care of the browser language and other local factors. So it might not always be a good idea to mess around with that.
AFAIK,没有其他方法可以完成您想做的事情。另外,请记住,原生 JavaScript 对话框的文本也会考虑浏览器语言和其他本地因素。因此,搞砸它可能并不总是一个好主意。
回答by Peter-Paul van Gemerden
Well, yes and no (no pun intended). You can use this:
好吧,是和否(没有双关语意)。你可以使用这个:
if (confirm("Would you like an alert?")) {
alert("Here you go!");
}
The confirmfunctionwill return trueif the user pressed "OK", or falseif "Cancel" was pressed. This is pure JavaScript (DOM level 0), so all browsers support this.
如果用户按下“确定”或按下“取消” ,该confirm函数将返回。这是纯 JavaScript(DOM 级别 0),因此所有浏览器都支持此.truefalse
However, as far as I know, there is no way to customize the labels of the buttons. So you're stuck with the defaults ("Cancel" and "OK" on most browsers, if set to English).
但是,据我所知,没有办法自定义按钮的标签。因此,您一直使用默认设置(大多数浏览器上的“取消”和“确定”,如果设置为英语)。
回答by Chuck Norris
No, you can't change Javascript's standart confirmation box buttons.
不,您不能更改 Javascript 的标准确认框按钮。
I suggest you to use Jquery UI dialog.
我建议您使用Jquery UI 对话框。
回答by BartekR
You can do it, but only in IE: Javascript Confirm popup Yes, No button instead of OK and CancelAFAIK Other browsers doesn't support it
您可以这样做,但仅限于 IE:Javascript Confirm popup Yes, No button 而不是 OK 和 CancelAFAIK 其他浏览器不支持
<script language="javascript">
function window.confirm(str) {
execScript('n = msgbox("' + str + '","4132")', "vbscript");
return (n == 6);
}
</script>
If you don't want jQueryUI, but think of a plugin - try http://www.abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/#demo(also mentioned in linked answer)
如果你不想要 jQueryUI,但想一个插件 - 试试http://www.abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/#demo(也在链接的答案中提到)
回答by nageen nayak
You can't change confirm box text but you can create your own.
您无法更改确认框文本,但可以创建自己的文本。
see here example :- Display Yes and No buttons instead of OK and Cancel in Confirm box?
请参阅此处的示例:-在确认框中显示是和否按钮而不是确定和取消?
i think you will get the idea about custom confirm box.
我想您会了解自定义确认框。

