我正在尝试通过 getJSON 方法获取账单金额。虽然我收到了金额,但无法发布。代码如下。
形式
<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);
});
});
在我的 Controller 中
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);
我确实得到了金额值,但是当我这样做时
getAmount = jQuery('.due').val(res);
alert(getAmount);
我得到[对象对象]
问题出在哪里?为什么该值没有出现在表单中?寻求指导。提前致谢。
最佳答案
您使用了 jQuery('.due'),但这是一个 div 元素。您需要执行以下方法来设置表单输入值,
jQuery('.due input').val(res);
它将金额设置为文本框值。
关于javascript - 在 ruby on Rails 中使用 jquery getJSON 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12274463/