0

有人可以帮助以下代码吗?这似乎是不正确的。

如何从回调中返回布尔值,并检查是否可以继续。

在我看来,布尔值始终为 true,并且永远不会返回 false。

还请描述代码在您更正后如何对我起作用。谢谢你。

$("#divContent").on('blur', "input[id^='cak4premium0']", function () {
    var selectedPayoutFreq = $('input[name=SelectedPayoutFrequency]:checked').val();

    if (typeof selectedPayoutFreq === "undefined") {
        alert("Please select Payout Frequency");
        return;
    }

    var selectedValue = $(this).val();

    if (selectedValue = '') {
        alert("Please enter Premium");
        return;
    }

    var premiumValid = checkPremiumAmount();

    if (premiumValid) {
        var url = "/Annuity/Home/CalculateAnnuityPayout";
        var data = GetAnnuityMainPlan();
        data.SelectedPayoutFrequency = selectedPayoutFreq;
        $.post(url, data, DisplayCountAnnuity);
    }

});

function checkPremiumAmount() {
    var url = "/Annuity/Home/GetAnnuityMinMaxPremium";
    var data = GetAnnuityMainPlan();
    var currentPlan = $('.selectPlan').val();
    var retValue = true;
    $.post(url, data, null).done(function (data) {
        minPremium = data.MinPremium;
        maxPremium = data.MaxPremium;
        var premium = data.Premium;
        if (premium < minPremium) {
            if (currentPlan == 'CAK4') {
                alert("The minimum Premium is $" + minPremium);
                $("input[id^='cak4premium0']").val(minPremium);
                retValue = false;
            }
            else if (currentPlan == 'GAK4') {
                alert("For single premium less than " + minPremium + ", annuity installment will be paid yearly.");
                $("input[id^='cak4premium0']").val(minPremium);
                retValue = false;
            }
        }


        if (premium > maxPremium) {
            alert("The maximum Premium is " + maxPremium);
            $("input[id^='cak4premium0']").val(maxPremium);
            retValue = false;
        }

        return retValue;
    });

}
4

1 回答 1

1

jquery - 从回调函数(在发布请求中)返回值到其内部的函数中?

除非您进行同步 AJAX 调用(您可能不想这样做),否则您根本做不到。

如果此函数在您的代码中的多个位置使用,您最好的选择可能是允许它接收一个函数。

这样一来,您无需依赖函数返回的结果在某些代码中使用,而是直接将代码传入,因此可以确保能够使用响应。

var my_form = $('#my_form');

my_form.submit( valid_pass_sett );

function valid_pass_sett() {
    //code to remove errors left over from previous submissions - snipped
    pass_old = $('input[name=pass_old]').val();
    pass_new = $('input[name=pass_new]').val();
    pass_confirm_new = $('input[name=pass_confirm_new]').val();

    validate('password', pass_new, pswd_validation_callback); // async validation

    return false;  // cancel form submission
}

function validate(request_type, request_text, callback ) {
    $.post("http://www.example.com/ajax/validate.php",{
        type: request_type, 
        text: request_text
    }, callback );
}

function pswd_validation_callback( data ) {
    if ( data === 'valid' ) {
         // if valid, call the native .submit() instead of the jQuery one
        my_form[ 0 ].submit();
    } else {
         // Otherwise do your thing for invalid passwords.
         // The form has already been canceled, so no concerns there.
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();
    }
}
于 2013-06-06T06:18:18.067 回答