简体   繁体   中英

Pass rails form variables to coffescript

I have a form in a rails app that has 3 inputs. I need to pass the 3 inputs to a CoffeeScript function. How can I pass these variables to the coffeescript as soon as they are input. Here is my view.

<%= 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>

your question is a bit incomplete, are you using any js framework like jQuery or Stimulus? If you want to do pure coffee/javascript then you'd go for the traditional getElementsByName :

amount = document.getElementsByName('amount')[0].value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM