从Jquery回调函数

时间:2016-02-18 20:30:53

标签: javascript jquery jsf-2

我正在使用JSF / Javascript / Jquery使用Stripe构建信用卡付款表单。以下是代码:

JSF表格

<h:form id="payment-form">
:
    :
    Input text fields
    :                       
    <h:commandButton id="submitBtn" value="Submit Payment" styleClass="button round blue" action="#{userBean.submitPayment}">
    </h:commandButton>
</h:form>

Jquery中的提交事件处理程序如下 -

function stripeResponseHandler(){
    alert("in responseHandler");

    var f = $("#payment-form");
// Token contains id, last4, and card type:
var token = response['id'];

    // Insert the token into the form so it gets submitted to the server
      f.append("<input type='hidden' name='stripeToken' value='" + token + "' />");

// re-enable the submit button
    $("#payment-form\\:submitBtn").attr("disabled", false);

// Submit the form
    //$("#payment-form")[0].submit();
    return false;
}

function reportError(msg) {
    // Show the error in the form:
    $('#payment-errors').text(msg).addClass('alert alert-error');
    // re-enable the submit button:
    $("#payment-form\\:submitBtn").prop('disabled', false);
    return false;
}

$(document).ready(function(){
    $("#payment-form").submit(function(event) {
        alert("in eventHandler");

        // Flag variable:
        var error = false;

        // disable the submit button to prevent repeated clicks:
        $("#payment-form\\:submitBtn").attr("disabled", "disabled");

        // Get the values:
        // Validate the fields
        // Validate other form elements, if needed!

        // Check for errors:
        if (!error) {
            //alert("Getting the token");
            // Get the Stripe token:
            Stripe.card.createToken({
                number: ccNum,
                cvc: cvcNum,
                exp_month: expMonth,
                exp_year: expYear
            }, stripeResponseHandler);

        }
        event.preventDefault();
    });
});

(从Stripe获取的代码与JSF表单集成)

当从stripeResponseHandler调用时,上面的代码不会调用h:commandButton的action参数中提到的backing bean方法,这是来自Stripe的异步响应的回调。

有人可以说清楚上述代码中的错误。

0 个答案:

没有答案