jquery preventDefault on alert,如果alert = ok,则发送

时间:2015-11-27 17:34:54

标签: javascript jquery html dom

我正在尝试创建提醒,以确保用户正在提交正确的信息,以及是否“确定”。单击而不是取消,单击链接并发送<a>。我几乎已经实现了它,警报激活,但是如果单击确定则不激活。不幸的是,我不是一个js巫师,但是......

编辑:

  

click =&gt; preventDefault =&gt;警告(是,否)如果是(发送),如果没有。

<script>
jQuery(document).ready(function($){

$("input.btn-default").click(function(e){
    e.preventDefault();
    var answer=confirm('Are you sure that you want to do this?');
    if(answer == true){
        ///not sure how to remove the prevent default and send link?!
    }
    else{
       return;
    }
    });
});

</script>

2 个答案:

答案 0 :(得分:2)

如果用户取消确认对话框,请使用return false;尝试使用以下代码;当用户点击确定时,请使用window.open( $(this).att('href') );打开链接:

$("input.btn-default").click(function(e)
{
    e.preventDefault();

    if( confirm('Are you sure that you want to do this?') )
    {
        window.open( $(this).att('href') );
    }
    else
    {
       return false;
    }
});

希望这有帮助。

答案 1 :(得分:0)

confirm()是模态的,因此您只需要检查answer

jQuery(document).ready(function ($) {
    $("input.btn-default").click(function (e) {
        var answer = confirm('Are you sure that you want to do this?');
        if (!answer) {
            e.preventDefault();
        }
    });
});