Rails表单提交处理程序未执行

Rails表单提交处理程序未执行

本文介绍了Rails表单提交处理程序未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试集成平衡付款"功能,以允许用户保存其信用卡信息,以便以后使用.相关的视图代码和javascript文件如下,但是我在javascript文件中定义的提交处理程序时遇到了麻烦.

I'm trying to integrate Balanced Payments to allow users to save their credit card information for use at a later time. The relevant view code and javascript files are as follows, but I'm having trouble with the submit handler defined in the javascript file.

balanced_card.js

var marketplaceUri = '{MY MARKETPLACE URI}';
var requestBinUrl = '{FORM SUBMISSION PATH}'

var debug = function(tag, content) {
    $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};

try {
    balanced.init(marketplaceUri);
} catch (e) {
    debug('code', 'balanced.init error!');
}

function balancedCallback(response) {
    var tag = (response.status < 300) ? 'pre' : 'code';
    debug(tag, JSON.stringify(response));
    switch(response.status) {
        case 201:
            console.log(response.data);
            var $form = $("#credit-card-form");
            var card_token_uri = response.data['uri'];
            $('<input>').attr({
                type: 'hidden',
                value: card_token_uri,
                name: 'balancedCreditCardURI'
            }).appendTo($form);
            $form.attr({action: requestBinUrl});
            $form.get(0).submit();
        case 400:
            console.log(response.error);
        case 404:
            console.log(response.error);
            break;
    }
}

var tokenizeCreditCard = function(e) {
    e.preventDefault();

    var $form = $('#credit-card-form');
    var creditCardData = {
        card_number: $form.find('.cc-number').val(),
        expiration_month: $form.find('.cc-em').val(),
        expiration_year: $form.find('.cc-ey').val(),
        security_code: $form.find('.cc-csc').val()
    }

    balanced.card.create(creditCardData, balancedCallback);
};

$('#credit-card-form').submit(tokenizeCreditCard);

credit_card.html.haml

= javascript_include_tag 'https://js.balancedpayments.com/v1/balanced.js'

= javascript_include_tag 'balanced_card'

= render :partial => 'crdtcard'

_crdtcard.html.haml

%h3 Credit Card
= form_tag '#', :method => :post, :id => "credit-card-form" do
  = label_tag "Card Number"
  = text_field_tag "cc-number", nil, :placeholder => "Enter Credit Card Number", :class => "cc-number", :autocomplete => "off"
  = label_tag "Expiration"
  = text_field_tag "cc-em", nil, :placeholder => "Expiration Month", :class => "cc-em", :autocomplete => "off"
  = text_field_tag "cc-em", nil, :placeholder => "Expiration Year", :class => "cc-ey", :autocomplete => "off"
  = label_tag "Security Code"
  = text_field_tag "cc-csc", nil, :placeholder => "Security Code", :class => "cc-csc", :autocomplete => "off"
  = submit_tag "Submit", :name => "submit"

当我用Firebug调试时,tokenizeCreditCard似乎从未被调用.

tokenizeCreditCard never seems to get called when I debug with Firebug.

推荐答案

您错过了将绑定包装在onDomready事件中的问题:

You missed wrapping your bind inside a onDomready event:

$(function(){
  $('#credit-card-form').submit(tokenizeCreditCard);
})

这篇关于Rails表单提交处理程序未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:42