如何使用jQuery开发“是”、“否”确认框

10

如何使用jQuery或其他方法开发带有“是”和“否”按钮的确认对话框?我需要在单击提交按钮时出现该确认。


3
你确定要一个 "你真的确定吗!" 按钮吗? - John Hartsock
2
来自同一用户的重复内容:https://dev59.com/OE7Sa4cB1Zd3GeqP69EB#3165601。 - Kris van der Mast
3个回答

45

使用浏览器原生的确认对话框。

if(confirm("Are you sure?"))
{
    //Ok button pressed...
}
else
{
    //Cancel button pressed...
}

根据您是否希望覆盖对象的操作继续执行,返回true或false,例如已单击的链接。 - Crypth

4

我有一个解决方案,对我来说很有效。

     $.alerts.okButton = ' Yes ';
     $.alerts.cancelButton = ' No ';                  
     jConfirm('Are you sure??',  '', function(r) {
       if (r == true) {                    
          //Ok button pressed...
       }  
     });

1
未捕获的类型错误:无法设置未定义的 'okButton' 属性。 - AlxVallejo
你是否正确配置了jQuery?!! - Sushanth CS

0

这将为您提供本地JS和jQuery版本:

function confirm_action( strMessage ) {
    strMessage = (typeof strMessage !== 'undefined') ? strMessage : 'Are you sure you want to do this?';
    return !!confirm( strMessage );
}

$.fn.confirm_action = function ( strMessage ) {
    $(this).click( function(){
        return confirm_action( strMessage );
    });
};

有两种不同的使用方式。这些适用于链接或提交输入。

JavaScript:

<input type="submit" formaction="process.php" onclick="return confirm_action();" />

jQuery:

$('#mybutton').confirm_action();

请注意,在任何版本中,您都可以通过传递字符串参数来自定义确认消息。

这不会给出是/否的答案。 - Spencer Sullivan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接