3

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

我已经使用了该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

4 回答 4

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>
于 2012-09-01T09:58:35.837 回答
2

这就是你真正需要的:

<script type="text/javascript">

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

</script>
于 2012-09-01T10:08:31.270 回答
0

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

function forceNonDefaultOk() {
    return ( ! confirm( 'Keep working' ) );
}
于 2013-04-23T15:14:18.460 回答
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;
    }   
}
于 2012-09-01T09:58:40.567 回答