getting data using jquery getJSON in ruby on rails

心不动则不痛 提交于 2020-01-04 09:05:12

问题


i am trying to get amount of a bill through getJSON method. Though i am getting the amount, i am not able to post it. The code is as follows.

in the form

<div class="bill">
<%= f.collection_select :customer_bill_id, CustomerBill.all,:id,:id, {:prompt => "Select Customer bill"} %></div><br />

<div class= "due"><%= f.input :bill_due_amount, :label => "Bill Due Amt.", :input_html => { :size => 20} %> </div><br />

the js

jQuery(".bill select").bind("change", function() {
        var data = {
        customer_bill_id: jQuery(this).val()  
      }
      jQuery.getJSON(
         "/money_receipts/get_due_amount",
        data,
        function(data){
          var res = "";
      res =  parseFloat(data[0]);
      getAmount = jQuery('.due').val(res);
        });      
    });

and in the controller i have

def get_due_amount
    @due_amount = CustomerBill.find_all_by_id(params[:customer_bill_id]).map{|bill| [bill.bill_due_amount]}if params[:customer_bill_id]
    respond_to do |format|
      format.json { render json: @due_amount }
    end
  end

if i do alert(res); i do get the amount value but when i do

getAmount = jQuery('.due').val(res);
        alert(getAmount);

i get [object Object]

what seems to be the problem? why is the value not appearing in the form? seeking guidance. Thanks in advance.


回答1:


You used jQuery('.due'), but this is a div element. Fou need to do the following way to set form input value,

jQuery('.due input').val(res); 

It will set the amout to textbox value.



来源:https://stackoverflow.com/questions/12274463/getting-data-using-jquery-getjson-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!