Rails nested_form查找元素已添加(Rails nested_form find element added)

我在表单中有一个嵌套表单,单击一个按钮会添加一堆字段供用户完成。

目前,当用户点击它时,我总结一个跟踪添加的字段数量的变量。 在添加的一堆东西上,我添加了一个按钮来删除它们(来自nested_form gem的link_to_remove)。 我想点击删除按钮,但由于某种原因它没有激活它。

这是视图上的代码:

= f.fields_for :guests do |guest_form| (...) = guest_form.link_to_remove t('actions.delete'), class: 'flex button button-general btn red-button delete-client-button form-rate delete-room-button', method: 'get' = f.link_to_add ((fa_stacked_icon "plus", base: "circle-thin", class: 'button-icon') + t('reservations.new.add_client')), :guests, class: 'button btn button-general green-button add-button new-client-button'

这是我的javascript:

$('.delete-client-button').click(function() { do something... });

但它没有做任何事情。

I have a nested form in a form, which on clicking on a button adds a bunch of fields for the user to complete.

At the moment, when the user clicks on it, I sum to a variable that tracks the amount of fields added. On the bunch of things added I add a button to remove them (a link_to_remove from the nested_form gem). I want to have an action on click on the remove button, BUT for some reason it is not activating it.

Here's the code on the view:

= f.fields_for :guests do |guest_form| (...) = guest_form.link_to_remove t('actions.delete'), class: 'flex button button-general btn red-button delete-client-button form-rate delete-room-button', method: 'get' = f.link_to_add ((fa_stacked_icon "plus", base: "circle-thin", class: 'button-icon') + t('reservations.new.add_client')), :guests, class: 'button btn button-general green-button add-button new-client-button'

Here's my javascript:

$('.delete-client-button').click(function() { do something... });

But it is not doing anything.

最满意答案

您必须委托该元素

$('body').on('click', '.delete-client-button', function() { do something... });

You have to delegate the element

$('body').on('click', '.delete-client-button', function() { do something... });Rails nested_form查找元素已添加(Rails nested_form find element added)

我在表单中有一个嵌套表单,单击一个按钮会添加一堆字段供用户完成。

目前,当用户点击它时,我总结一个跟踪添加的字段数量的变量。 在添加的一堆东西上,我添加了一个按钮来删除它们(来自nested_form gem的link_to_remove)。 我想点击删除按钮,但由于某种原因它没有激活它。

这是视图上的代码:

= f.fields_for :guests do |guest_form| (...) = guest_form.link_to_remove t('actions.delete'), class: 'flex button button-general btn red-button delete-client-button form-rate delete-room-button', method: 'get' = f.link_to_add ((fa_stacked_icon "plus", base: "circle-thin", class: 'button-icon') + t('reservations.new.add_client')), :guests, class: 'button btn button-general green-button add-button new-client-button'

这是我的javascript:

$('.delete-client-button').click(function() { do something... });

但它没有做任何事情。

I have a nested form in a form, which on clicking on a button adds a bunch of fields for the user to complete.

At the moment, when the user clicks on it, I sum to a variable that tracks the amount of fields added. On the bunch of things added I add a button to remove them (a link_to_remove from the nested_form gem). I want to have an action on click on the remove button, BUT for some reason it is not activating it.

Here's the code on the view:

= f.fields_for :guests do |guest_form| (...) = guest_form.link_to_remove t('actions.delete'), class: 'flex button button-general btn red-button delete-client-button form-rate delete-room-button', method: 'get' = f.link_to_add ((fa_stacked_icon "plus", base: "circle-thin", class: 'button-icon') + t('reservations.new.add_client')), :guests, class: 'button btn button-general green-button add-button new-client-button'

Here's my javascript:

$('.delete-client-button').click(function() { do something... });

But it is not doing anything.

最满意答案

您必须委托该元素

$('body').on('click', '.delete-client-button', function() { do something... });

You have to delegate the element

$('body').on('click', '.delete-client-button', function() { do something... });