javascript / window.confirm 函数的问题

Problems with javascript / window.confirm function

我有一个调用 checkTax() 函数的 HTML 按钮。 该功能应该在单击“确定”时确认并继续提交表单,或者取消提交并将用户重定向到其他页面。

这是函数:

function checkTax () {
    if ( CUSTTAXRATE == 0 ) {
        var r = confirm("Your current tax rate is 0.\n\nIf this is correct click OK to continue.\n\nIf this needs to be adjusted, click CANCEL and visit the quote set up page under DEALER RESOURCES tab.");
        if (r == true){
            return true;
        }
        else {
        <!---   return false; --->
             window.location.replace("index.cfm?action=retailQuote.settings");

        }
    }

}

我已经尝试过取消提交或重定向提交,但我都无法正常工作。这两种方式仍然提交表格并继续。 我做错了什么??

确保在按钮的 onclick 属性中使用 return 语句。

<button type="submit" onclick="return checkTax();">Submit</button>

否则,函数中的 return 值将被忽略,并且在 returns false.

时不会阻止表单提交

为了简化您的操作,我已尝试完成上述答案。 请找到以下代码:

<body>
<form action="">
    <input type=text id="t1">
    <button type="submit" onclick="return checkTax();">Submit</button>
</form>

<script type="text/javascript">
    function checkTax() {
        var CUSTTAXRATE = document.getElementById("t1");
        if (CUSTTAXRATE == 0) {
            var r = confirm("Your current tax rate is 0.\n\nIf this is correct click OK to continue.\n\nIf this needs to be adjusted, click CANCEL and visit the quote set up page under DEALER RESOURCES tab.");
            if (r == true) {
                return true;
            } else {
                window.location
                        .replace("index.cfm?action=retailQuote.settings");
                return false;
            }
        }

    }
</script>