0

我正在尝试捕获表单上的提交按钮,并进行一些处理(在这种情况下,将信用卡数据发送到 Stripe)。我有一个 Order 类,当按下新的订单提交按钮时,它会捕获提交并将其发送到我的captureSubmitForStripe方法。那部分工作得很好,这是一个代码片段:

# orders.js.coffee:
jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  order.setupForm()

order =
  setupForm: ->
    $('#new_order').submit ->
      order.captureSubmitForStripe()

  captureSubmitForStripe: ->
    # do more stuff here...

我也想让编辑表单上的提交按钮也能做到这一点。我该如何编写它,以便它从我所在的任何表单中捕获提交按钮,或者至少从新表单和编辑表单中捕获提交按钮?

我已经尝试过这个,除其他外,它不起作用:

order =
  setupForm: ->
    $('#new_order').submit ->
      order.captureSubmitForStripe()
    $('#edit_order').submit ->
      order.captureSubmitForStripe()
4

1 回答 1

1

使用没有问题,$('#new_order')因为这是form_for用于新对象时表单的默认 id。对于持久化对象,id 变为edit_order_123123 是对象的 id。我建议您为新表单和编辑表单使用一个类

# set the class
form_for @order, html: { class: 'order-form' } do
  ...

# then just use the class to select the form
$('.order-form').submit -> order.captureSubmitForStripe()
于 2013-02-04T05:53:01.897 回答