最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect confirm() choice? - Stack Overflow

programmeradmin5浏览0评论

I have this jquery code that generates a yes/no dialog box when the user click a button.

$('#btnFinalize').click(function()
{
   confirm("Are you sure?");            
});

But how can I detect if the user click yes/no?

I have this jquery code that generates a yes/no dialog box when the user click a button.

$('#btnFinalize').click(function()
{
   confirm("Are you sure?");            
});

But how can I detect if the user click yes/no?

Share Improve this question edited Sep 5, 2012 at 17:02 Gordon 317k76 gold badges546 silver badges565 bronze badges asked May 20, 2012 at 12:08 John Micah Fernandez MiguelJohn Micah Fernandez Miguel 5243 gold badges12 silver badges29 bronze badges 2
  • if this is all your code, all you need is to add a return: .click(function() { return confirm("Are you sure?") }) – georg Commented May 20, 2012 at 12:26
  • see plugin and example here: stackoverflow./questions/6457750/form-confirm-before-submit/… – user1662816 Commented Sep 11, 2012 at 12:20
Add a ment  | 

5 Answers 5

Reset to default 5

confirm() returns true if the user clicked "yes" ; and false if he clicked "no".

So, basically, you could use something like this :

if (confirm("Are you sure?")) {
    // clicked yes
}
else {
    // clicked no
}

Check the return value of confirm(); it will be true or false:

if(confirm('Are you sure?! DAMN sure?')) {
    // yes
}
else {
    // no
}

Try this

$('#btnFinalize').click(function()
{
   var reply = confirm("Are you sure?");         

    if (reply)
    {
        // your OK
    }
    else
    {
        // Your Cancel
    }
});

confirm() will return true or false, depending on the answer. So you can do something like:

$('#btnFinalize').click(function()
{
   var finalizeAnswer = confirm("Are you sure?");
   if(finalizeAnswer) {
       // do something if said yes
   } else {
       // do something if said no
   }
});

'Confirm()' returns 'true' if you press 'yes', and 'false' for 'no'.

发布评论

评论列表(0)

  1. 暂无评论