返回不停止表单提交(javascript)

时间:2014-04-20 23:45:57

标签: javascript forms

我目前正试图阻止表单通过confirm()提交,但是当我单步执行代码时它确实返回false,它似乎并不能阻止代码提交。我在确认购买中引用了被叫回报,我的问题是它只将false返回到datevalidate函数而不是onsubmit?

function datevalidate() {
var dateNow = new Date();
var month = document.getElementById("expmon").value;
var year = document.getElementById("expyear").value;

var currentDate= new Date(year,month);

if (currentDate < dateNow){
alert("Your card has expired please enter valid details")
return false;
}

confirmpurchase();
}

function confirmpurchase(){
cPurchase = confirm("Are you sure you want to proceed?");
if (cPurchase==false){
return=false;}

}

2 个答案:

答案 0 :(得分:1)

您的datevalidate()在调用确认功能时不会返回任何内容。您需要返回confirmpurchase()值。

//.....
if (currentDate < dateNow){
        alert("Your card has expired please enter valid details")
        return false;
    }

    return confirmpurchase(); // need to return the confirm value
    // an easier option is return confirm("Are you sure you want to proceed?");
}

此外,如果有效,您的confirmureurchase()需要返回true(例如,最后添加return true)。

答案 1 :(得分:0)

我建议使用event.preventDefault()

function datevalidate(event)
    if (!confirmpurchase) {
        event.preventDefault()
    }
相关问题