我有这个代码检查数据库中的电子邮件地址。问题是,即使result
值为1
或-1
,表单仍会提交。这是HTML
:
<form name="myForm" target="_self" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- If using a Business or Company Logo Graphic, include the "cpp_header_image" variable. -->
<input type="hidden" name="cmd" value="_donations">
<!-- Replace "business" value with your PayPal Email Address or your Merchant Account ID -->
<input type="hidden" name="business" value="webmaster@gmail.com">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="cn" value="">
<!-- Purpose field -->
<input type="hidden" name="item_name" value="Your Donation">
<!-- -->
<label>Email Address</label>
<input type="text" id="email" name="custom">
<br><br>
<label>Donation</label>
<select name="amount">
<option value="10">USD 10</option>
<option value="30">USD 30</option>
</select>
<br>
<!-- -->
<input id="check_email" type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="Donate to!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
JQuery
$(document).ready(function() {
//function to check email availability
var check_email_availability = function (){
var email_check = $.Deferred();
//use ajax to run the check
$.post("/validate_email.php", { email: email },
function(result){
//if the result is 1
if(result == 1){
//show that the email does not exist available
alert("Email does not exist in db");
}if(result == 0){
//show that the email found in the system
email_check.resolve();
}if(result == -1){
//show that the email format is invalid
alert("Email Format invalid");
}
});
return email_check;
} //function end
$("#check_email").click(function(e) {
e.preventDefault();
if($("#email").val() != ""){
( check_email_availability()).done($("form[name='myForm']").submit());
}
}); //click event end
});
表格假设在result == 0
时提交。我更喜欢使用$.Deferred()
请提前帮助和谢谢。
答案 0 :(得分:1)
您需要将函数引用传递给.done()
,如下所示:
check_email_availability().done(function() {
$("form[name='myForm']").submit();
});
你这样做的过程就是你立即执行.submit()
并将返回值传递给.done()
(你发现的那个)不是你想要的时间。
您还可以更改代码以使用$.post()
返回的承诺,而不是像这样创建自己的承诺:
$(document).ready(function() {
//function to check email availability
function check_email_availability() {
//use ajax to run the check
return $.post("/validate_email.php", { email: email });
}
$("#check_email").click(function(e) {
e.preventDefault();
if ($("#email").val()) {
check_email_availability().then(function(result) {
if (result === 0) {
$("form[name='myForm']").submit();
} else if (result == 1) {
alert("Email does not exist in db");
} else if (result == -1) {
alert("Email Format invalid");
}
});
}
}); //click event end
});
答案 1 :(得分:1)
您需要在完成回调中执行表单提交,如果您在延迟解决之前正在执行表单提交。即您首先调用表单提交然后尝试将从该表返回的值作为参数传递给done()
check_email_availability().done(function(){
$("form[name='myForm']").submit()
});
同样来自check_email_availability
你应该只返回一个promise对象,而不是像return email_check.promise();