在 2checkout 中提交之前验证表单

Validate form before submit in 2checkout

我想防止表单被提交所以我尝试了下面的代码,这通常有效。但是当我使用 2checkout js(https://www.2checkout.com/static/checkout/javascript/direct.js) 它不起作用。

<form onSubmit="validate(); return false;" method='post'>

这里是jsfiddlehttp://jsfiddle.net/0a0th3fy/1/

下面是示例代码

<form onSubmit="validate(); return false;" action='https://www.2checkout.com/checkout/purchase' method='post'>
    <input type='hidden' name='sid' value='1303908' />
    <input type='hidden' name='mode' value='2CO' />
    <input type='hidden' name='li_0_type' value='product' />
    <input type='hidden' name='li_0_name' value='invoice123' />
    <input type='hidden' name='li_0_price' value='25.99' />
    <input id="fullname" name='card_holder_name' value='' />
    <input type='hidden' name='street_address' value='123 Test Address' />
    <input type='hidden' name='street_address2' value='Suite 200' />
    <input type='hidden' name='city' value='Columbus' />
    <input type='hidden' name='state' value='OH' />
    <input type='hidden' name='zip' value='43228' />
    <input type='hidden' name='country' value='USA' />
    <input type='hidden' name='email' value='example@2co.com' />
    <input type='hidden' name='phone' value='614-921-2450' />
    <input name='submit' type='submit' value='Checkout' />
</form>

和javascript

function validate(){
    alert("validating");
    if($("#fullname").val()==""){
        alert("enter name");   
        return false;
    }
    else {
        return true;
    }    
}

你为什么不使用 jQueryformSubmit event

试试这个:

$("form").submit(function (event) {
    alert("validating");
    if ($("#fullname").val() == "") {
        alert("enter name");
        $("#tco_lightbox").hide();
        event.preventDefault();
    } else {
        return true;
    }

});

详细了解 event.preventDefault()

我试过你的代码,它成功了。 您确定包含 jquery 吗?

    
    function validate(){
    alert("validating");
    if($("#fullname").val()==""){
        alert("enter name");   
        return false;
    }
    else {
        return true;
    }    
}
I tried your code, and it's worked.
Are you sure you include jquery?

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

<p>When you submit the form, a function is triggered which alerts some text.</p>

<form onSubmit="validate(); return false;" action='' method='post'>
    <input type='hidden' name='sid' value='1303908' />
    <input type='hidden' name='mode' value='2CO' />
    <input type='hidden' name='li_0_type' value='product' />
    <input type='hidden' name='li_0_name' value='invoice123' />
    <input type='hidden' name='li_0_price' value='25.99' />
    <input id="fullname" name='card_holder_name' value='' />
    <input type='hidden' name='street_address' value='123 Test Address' />
    <input type='hidden' name='street_address2' value='Suite 200' />
    <input type='hidden' name='city' value='Columbus' />
    <input type='hidden' name='state' value='OH' />
    <input type='hidden' name='zip' value='43228' />
    <input type='hidden' name='country' value='USA' />
    <input type='hidden' name='email' value='example@2co.com' />
    <input type='hidden' name='phone' value='614-921-2450' />
    <input name='submit' type='submit' value='Checkout' />
</form>


</body>
</html>