将 rails 表单变量传递给 coffeescript
Pass rails form variables to coffescript
我在 rails 应用程序中有一个表单,它有 3 个输入。我需要将 3 个输入传递给 CoffeeScript 函数。如何在输入这些变量后立即将它们传递给 coffeescript。这是我的看法。
<%= form_tag( '/welcome/how_much', post: true, remote: true) do %>
<span id="questions">
<h5 class="label">Estimated new home cost?</h5>
<%= text_field(:amount, {id: "house_amount", placeholder: "ex. 100,000"}, class: "form-control form-control-lg") %>
</span>
<span id="questions">
<h5 class="label">Estimated payment for a new home?</h5>
<%= text_field(:high_rent, {id: "high_rent", placeholder: "ex. 1,200"}, class: "form-control form-control-lg") %>
</span>
<span id="questions">
<h5 class="label">Current Monthly Rent?</h5>
<%= text_field(:current_rent, {id: "current_rent", placeholder: "ex. 800"}, class: "form-control form-control-lg") %>
</span>
<%= submit_tag("See how quickly you can buy a home", data: {'data-toggle' => "modal", 'data_target' => "#savings_modal"}, class: "btn btn-success btn-lg") %>
<!-- Modal for sign-up -->
<div class="modal" id="savings_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<h3 class="modal-title" id="savingsModalTitle">You could be ready to buy in <%= @months_to_buy %> months</h3>
<h5 class="modal-title">and have <%= @total_savings %>* to put towards a down payment & a drastically increased credit score using LikeHome</h5>
<div class="modal-body">
<h4>Sign-up Now to get started!</h4>
<%= render '_tenant_signup_modal_form.html.erb' %>
</div>
</div>
</div>
</div>
你的问题有点不完整,你用的是jQuery或Stimulus之类的js框架吗?如果你想做纯 coffee/javascript 那么你会选择传统的 getElementsByName
:
amount = document.getElementsByName('amount')[0].value;
我在 rails 应用程序中有一个表单,它有 3 个输入。我需要将 3 个输入传递给 CoffeeScript 函数。如何在输入这些变量后立即将它们传递给 coffeescript。这是我的看法。
<%= form_tag( '/welcome/how_much', post: true, remote: true) do %>
<span id="questions">
<h5 class="label">Estimated new home cost?</h5>
<%= text_field(:amount, {id: "house_amount", placeholder: "ex. 100,000"}, class: "form-control form-control-lg") %>
</span>
<span id="questions">
<h5 class="label">Estimated payment for a new home?</h5>
<%= text_field(:high_rent, {id: "high_rent", placeholder: "ex. 1,200"}, class: "form-control form-control-lg") %>
</span>
<span id="questions">
<h5 class="label">Current Monthly Rent?</h5>
<%= text_field(:current_rent, {id: "current_rent", placeholder: "ex. 800"}, class: "form-control form-control-lg") %>
</span>
<%= submit_tag("See how quickly you can buy a home", data: {'data-toggle' => "modal", 'data_target' => "#savings_modal"}, class: "btn btn-success btn-lg") %>
<!-- Modal for sign-up -->
<div class="modal" id="savings_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<h3 class="modal-title" id="savingsModalTitle">You could be ready to buy in <%= @months_to_buy %> months</h3>
<h5 class="modal-title">and have <%= @total_savings %>* to put towards a down payment & a drastically increased credit score using LikeHome</h5>
<div class="modal-body">
<h4>Sign-up Now to get started!</h4>
<%= render '_tenant_signup_modal_form.html.erb' %>
</div>
</div>
</div>
</div>
你的问题有点不完整,你用的是jQuery或Stimulus之类的js框架吗?如果你想做纯 coffee/javascript 那么你会选择传统的 getElementsByName
:
amount = document.getElementsByName('amount')[0].value;