twitter-bootstrap bootbox.confirm() 不能正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23357356/
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
bootbox.confirm() not working properly
提问by Imran Chaudhary
What is the problem with bootbox.confirm()?This is my code:
bootbox.confirm() 有什么问题?这是我的代码:
var record = bootbox.confirm("Are you sure you want to delete", function (res) {
if (res == true) {
return true;
} else {
return false;
}
}
});
It doesn't wait for confirmation and return true by default. if is use return false at the end to stop it from disappearing
它不等待确认并默认返回 true。如果在最后使用 return false 来阻止它消失
var record = bootbox.confirm("Are you sure you want to delete",
function (res) {
if(res == true)
{
return true;
}
else
{
return false;
}
}
});
return false;
then it doesn't show any action against of clicking of "OK" or "cancel"
然后它不会显示任何针对单击“确定”或“取消”的操作
if i use any simple alert in true section to check behaviour it enters in true section and show simple alert and show required value of clicked button but if use return true in same section but it doesnt work
如果我在 true 部分使用任何简单警报来检查它在 true 部分中输入的行为并显示简单警报并显示单击按钮的所需值但如果在同一部分使用返回 true 但它不起作用
var record = bootbox.confirm("Are you sure you want to delete", function (res) {
if (res == true)
alert('true');
else
alert('false')
}
});
return false;
回答by Imran Chaudhary
I got the solution , bootstrap.confirm() is asynchronous in behaviour so we can't stop its flow but we can change our logic to use it like
我得到了解决方案,bootstrap.confirm() 在行为上是异步的,所以我们不能停止它的流程,但我们可以改变我们的逻辑来使用它
first of all we have to stop its default behaviour and then use it as our requirment
首先,我们必须停止其默认行为,然后将其用作我们的要求
whatever we want to do place it under the true section of the code
无论我们想做什么,都将它放在代码的真实部分下
e.preventDefault();// used to stop its default behaviour
bootbox.confirm(Are you sure you want to delete ? , function (confirmed) {
if (confirmed == true) {
// place here what we want to do
__doPostBack('btnDelete', 'OnClick');
}
else
{
}
});
回答by Michael Goldshmidt
Might be easier to understand... We split client side and server side event. Upon confirm, we trigger server-side click event... Just in case you don't like
可能更容易理解......我们将客户端和服务器端事件分开。确认后,我们触发服务器端点击事件...以防万一你不喜欢
__doPostBack('btnDelete', 'OnClick')
Server side is omitted...
服务器端省略...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<style>
.hidden{
display:none;
}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/jquery-3.0.0.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/bootbox.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnTest" runat="server" Text="Confirm 1" OnClientClick="return confirmClick(this);" />
<asp:Button ID="btnReal" runat="server" Text="Actual 1" CssClass="real-button hidden" OnClick="btnTest_Click" />
</div>
<div>
<asp:Button ID="btnTest2" runat="server" Text="Confirm 2" OnClientClick="return confirmClick(this);" />
<asp:Button ID="btnReal2" runat="server" Text="Actual 2" CssClass="real-button hidden" OnClick="btnTest_Click" />
</div>
<div>
<asp:Button ID="btnTest3" runat="server" Text="Confirm 3" OnClientClick="return confirmClick(this);" />
<asp:Button ID="btnReal3" runat="server" Text="Actual 3" CssClass="real-button hidden" OnClick="btnTest_Click" />
</div>
</form>
</body>
</html>
<script>
function confirmClick(e) {
var result = bootbox.confirm({
message: "This is a confirm with custom button text and color! Do you like it?",
buttons: {
confirm: {
label: 'Yes',
className: 'btn-success'
},
cancel: {
label: 'No',
className: 'btn-danger'
}
},
callback: function (result) {
var real = $(e).parent().find('.real-button');
if (result && real)
{
console.log('This was logged in the callback: ' + result + real.val());
real.click();
}
}
});
return true;
}
</script>
回答by dreamweiver
There is a extra flower braces in your code just remove it.
您的代码中有一个额外的花括号,只需将其删除即可。
var record = bootbox.confirm("Are you sure you want to delete", function (res) {
if (res == true) {
return true;
} else {
return false;
}
//} // invalid closing brace
});
this could be one of the issue for your confirm box to not work.
这可能是您的确认框无法工作的问题之一。
Happy Coding :)
快乐编码:)

