0

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

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

这是js小提琴http://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;
    }    
}
4

2 回答 2

2

你为什么不使用jQuery's formSubmit event

试试这个:

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

});

了解有关event.preventDefault()的更多信息

于 2015-04-09T13:01:31.830 回答
0

我试过你的代码,它的工作。你确定你包括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>

于 2015-04-09T13:19:35.817 回答