Javascript 将rails表单变量传递给coffescript

Javascript 将rails表单变量传递给coffescript,javascript,html,ruby-on-rails,coffeescript,Javascript,Html,Ruby On Rails,Coffeescript,我在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>

我在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>

估计的新房成本?
新房子的预计付款?
现在的月租?
“模式”,“数据目标”=>“节省模式”},类别:“btn btn成功btn lg”)%>
几个月后你就可以买了
而且必须支付首付&使用LikeHome大幅提高信用评分
现在就注册开始吧!

您的问题有点不完整,您是否使用jQuery或Stimulus之类的js框架?如果您想使用纯咖啡/javascript,那么您可以使用传统的
getElementsByName

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

不确定,我对咖啡脚本还比较陌生。尝试编写一个脚本,验证输入字段中是否有数字,然后对输入运行一个小计算,以显示在模式中。我相信我运行的只是coffeescript,没有其他框架。此外,这是否会进入js文件?
amount=document.getElementsByName('amount')[0].value
将此作为错误抛出
未捕获类型错误:如果
文档中的getElementsByName('amount')
为空,则无法读取未定义的
的属性'value'。然后检查html文本字段的
name
属性是什么。好的,我创建了一个问题,充分解释了我的问题。你能看一下吗?