【问题标题】:How to pass html values in Ruby within a Javascript function如何在 Javascript 函数中在 Ruby 中传递 html 值
【发布时间】:2015-10-10 14:41:17
【问题描述】:

我有一个视图页面,用户在其中输入一个数字并单击生成以根据此输入在同一页面中获取一个表单。所以我在我的主视图中使用了 javascript:

 <div class="label-field-pair"><%= "#{t('total')} #{t('amount')}" %>:
    <%= form.text_field :total, :value =>precision_label(@total_pay.to_f)  , :disabled=> true , :id=>'total' %></div>


      <div class="label-field-pair">
        <label for="student_grade"><%= t('no_payments') %><span class="necessary-field">*</span> </label>
        <input type="number" min="1" max="12" step="1" value="1" name="no_payments" id="no_payments">      </div>
       <%= form.hidden_field :no_payments, :id=>'payments' %>
       <%= form.hidden_field :total_pay, :id=>'total_pay' %>

    <%#= submit_tag "", :value => "► #{t('generate')}", :class => "submit_button", :disable_with => "► #{t('please_wait')}" %>
    <%#= render :partial => 'distribute_payments', :locals => {:no_payments=>2, :total => 30000 , :guardian=>@guardian.id} %>
    <button type="button" id="gen" onClick="generate()" style="display: block">Generate</button>
    <div id="payments_distribute"></div>
   <%end%>



  <script type="text/javascript">

       function generate() {


          var t = document.getElementById("total").value;
          var n = document.getElementById("no_payments").value;
          j('#total_pay').val("<%= @total_pay %>");
         document.getElementById("gen").style.display = "none";

          <%="#{remote_function(:url => {:action => "distribute_payments", :id=>@guardian.id,:total_pay=>@total_pay}

          )}"%>
      }



  </script>

这是我的部分观点的行动:

  def distribute_payments
    @total_amount=params[:total]
      @no_payments=params[:no_payments]
      @total_payment=params[:total_pay]
     @guardian = Guardian.find(params[:id])
    @students=Student.find_all_by_sibling_id(@guardian.ward_id)

      render(:update) do |page|
            page.replace_html 'payments_distribute', :partial=>'distribute_payments', :total_pay=>@total_payment, :no_payments=>@no_payments
         end
  end

这里的问题是我需要将“no_payments”的值与参数一起传递给我的局部视图,而我不知道如何从我的 Javascript 函数 (remote_function) 中执行此操作。我试图将 render_partial 与提交按钮一起使用,但我无法在同一页面中打开部分视图。你可以在这里查看我的问题:Rails: Render Partial from controller

【问题讨论】:

    标签: javascript jquery ruby-on-rails ruby


    【解决方案1】:

    你需要使用 Ajax:

      <script type="text/javascript">
    
           function generate() {
    
    
              var t = document.getElementById("total").value;
              var n = document.getElementById("no_payments").value;
              var id="<%= @guardian.id %>"
              j('#total_pay').val("<%= @total_pay %>");
             document.getElementById("gen").style.display = "none";
    
    
                     j.ajax({
          type: 'POST' ,
          url: '<%=  url_for :action => "distribute_payments" %>',
          data : {
            'no_payments' :n,
            'total_payment' :t,
            'id' :id
          },
          beforeSend : function() {
          },
          success : function() {
            j("#payments_distribute").html(data)
          }});
          }
    
    
    
      </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多