Javascript 确认框,如果点击取消,则不会重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22114870/
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, if cancel is clicked, it will not reload the page
提问by Yanyan
Is there away that the confirm box appeared, if i clicked "ok" it will go to another page and if i clicked "cancel" it will just stay and the current page will not reload again? THANK YOU.
有没有出现确认框,如果我点击“确定”它会转到另一个页面,如果我点击“取消”它只会停留并且当前页面不会再次重新加载?谢谢。
采纳答案by Kuldeep Choudhary
function reload()
{
var r=confirm("Do you want to leave page!");
if (r)
{
//write redirection code
window.location = "http://www.yoururl.com";
}
else
{
//do nothing
}
}
call this function when you want to confirmation from user.........
当你想从用户那里确认时调用这个函数.......
回答by Suman Bogati
You can use confirm()
for this, which returns true
on okor false
on cancel.
您可以使用confirm()
此,它的回报true
在确定或false
上取消。
function myFunction(){
if(confirm("Would you like go to other page?")){
window.location = "http://yahoo.com";
}else{
alert('fine, if not want');
}
}
myFunction();
Updated
更新
UPDATED2
更新2
<button onclick="return logout()" >logout</button>
<script>
function logout(){
if(confirm("Would you like go to other page?")){
window.location = "failed.php";
}else{
//do your stuff on if press cancel
}
}
</script>
回答by Dev Life
You may try doing
你可以尝试做
<script>
function myfunction(){
if(confirm("The confirm message")){
youDoTheThingsHere();
return false;
}else{
console.log("I cancelled the dialog!");
return false;
}
}
</script>
I'm not sure about your certain situation and the code that is involved when you call the confirm, but this has worked for me. The other option you may look at as a last resort is using something like a bootstrap modal to trigger a "confirm" modal. With the bootstrap modal you can then style it how you want... hope I could help!
我不确定您的特定情况以及您致电确认时所涉及的代码,但这对我有用。您可能会认为作为最后手段的另一个选项是使用类似引导模式的东西来触发“确认”模式。使用引导模式,您可以按照自己的意愿设置样式......希望我能帮上忙!