在ruby on rails上使用jquery getJSON获取数据 [英] getting data using jquery getJSON in ruby on rails
本文介绍了在ruby on rails上使用jquery getJSON获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过getJSON方法获取账单金额。虽然我得到了金额,但我无法发布。代码如下。
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.
格式
<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 />
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);
});
});
在控制器中我有
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
如果我做 alert(res);
我确实得到金额值,但是当我做
if i do alert(res);
i do get the amount value but when i do
getAmount = jQuery('.due').val(res);
alert(getAmount);
我得到 [object Object]
似乎是什么问题?为什么价值没有出现在表格中?寻求指导。在此先感谢。
what seems to be the problem? why is the value not appearing in the form? seeking guidance. Thanks in advance.
推荐答案
您使用了jQuery('。due'),但这是一个div元素。你需要按照以下方式设置表单输入值,
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);
它会将amout设置为文本框值。
It will set the amout to textbox value.
这篇关于在ruby on rails上使用jquery getJSON获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文