Javascript使用确认取消表单提交

时间:2012-09-01 09:48:03

标签: javascript html

如何将取消按钮添加到警报窗口?

我使用了confirm()方法但是当点击提交按钮时会弹出确认窗口,但是当点击取消按钮时,表格的数据会被存储。

我只想点击取消按钮不存储数据并保留原来的表格。这是我的代码:

function submitdata()
{   
    var r=confirm("Are You Sure You Want To Proceed?");

    if(r==true)
    {
        alert("Record is saved");
    }
    else
    {
        alert("Cancelling Transaction");
        javascript:history.go(0);
    }   
}

4 个答案:

答案 0 :(得分:2)

<form action="" name="test" onsubmit="return submitdata();">
    <input type="text" />

    <input type="submit" />

</form>

<script type="text/javascript">

function submitdata() { 
    var r=confirm("Are You Sure You Want To Proceed?"); 
    if(r==true) { 
        alert("Record is saved"); 
    } else { 
        alert("Cancelling Transaction"); 
        javascript:history.go(0);
    }

}

</script>

答案 1 :(得分:2)

这就是你真正需要的:

<script type="text/javascript">

    function submitdata() { 
        return confirm("Are You Sure You Want To Proceed?"); 
    }

</script>

答案 2 :(得分:0)

<强>更新

function submitdata() {
    var r=confirm("Are You Sure You Want To Proceed?");

    if(r==true) {
        alert("Record is saved");
        return true;
    } else {
        alert("Cancelling Transaction");
        return false;
    }   
}

答案 3 :(得分:0)

扭转逻辑。但问题是奇怪的语言,所以最好使用自定义提示设置取消默认值。

function forceNonDefaultOk() {
    return ( ! confirm( 'Keep working' ) );
}