【问题标题】:Pass rails form variables to coffescript将rails表单变量传递给coffeescript
【发布时间】:2018-11-07 10:46:51
【问题描述】:

我在具有 3 个输入的 Rails 应用程序中有一个表单。我需要将 3 个输入传递给 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>

【问题讨论】:

    标签: javascript html ruby-on-rails coffeescript


    【解决方案1】:

    您的问题有点不完整,您使用的是 jQuery 或 Stimulus 等 js 框架吗?如果你想做纯咖啡/javascript,那么你会选择传统的getElementsByName

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

    【讨论】:

    • 不确定,我对咖啡脚本比较陌生。尝试编写一个脚本来验证一个数字是否在输入字段中,然后对输入运行一个小计算以显示在模态中。我相信我只是在运行咖啡脚本,没有其他框架。
    • 还有,这个会进js文件吗?
    • amount = document.getElementsByName('amount')[0].value;将此作为错误抛出Uncaught TypeError: Cannot read property 'value' of undefined
    • 如果 document.getElementsByName('amount') 在您的 javascript 控制台上为空,请检查您的 html 文本字段的 name 属性是什么。
    • 好的,我创建了一个完全解释我的问题的问题。你能看看吗? stackoverflow.com/q/50574878/3877058
    猜你喜欢
    • 2013-01-25
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多