簡體   English   中英

deferred.resolve()無法解析

[英]deferred.resolve() won't resolve

我有這段代碼檢查數據庫中的電子郵件地址。 問題是,即使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()請提前幫助和感謝。

您需要像這樣將函數引用傳遞給.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       
});

您需要在done回調中進行表單提交,在這種情況下,您要在解決延遲之前進行表單提交。 也就是說,您首先調用表單提交,然后嘗試將其返回的值作為參數傳遞給done()

check_email_availability().done(function(){
    $("form[name='myForm']").submit()
});

同樣,在check_email_availability您應該僅返回一個return email_check.promise();對象,而不是像return email_check.promise();那樣延遲的對象return email_check.promise();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM