[英]Ruby on Rails Javascript to Disable Text Field With Checkbox
[英]dynamically displaying data in text field ruby on rails
大家好,我正在嘗試動態選擇這里。 一旦我選擇了客戶,他的賬單中的總價值就會顯示在文本字段標簽中。
風景
jQuery(document).ready(function(){
jQuery(".customerid").bind("change", function() {
var data = {
customer_id: jQuery(".customerid :selected").val()
}
jQuery.ajax({
url: "get_cust_bill",
type: 'GET',
dataType: 'script',
data: data
});
});
});
</script>
<div class ="customerid"><%= f.label :customer_id %>
<%= f.collection_select :customer_id, Customer.all, :id, :name, options ={:prompt => "-Select a Customer"}, :class => "state", :style=>'width:210px;'%></div><br />
<div class ="customerbill">
<%= f.label :total_value, "Total Value" %>
<%= render :partial => "customerbill" %>
js.erb文件
jQuery('.customerbill').html("<%= escape_javascript(render :partial => 'customerbill') %>");
客戶部分
<% options = []
options = @cust_bill.total_value if @cust_bill.present? %>
<%= text_field_tag "total_value", options %>
在控制器中
def get_cust_bill
@cust_bill = CustomerBill.find_all_by_customer_id(params[:customer_id]) if params[:customer_id]
end
我覺得問題在於部分,我調用options
所以任何人都可以指導我如何在文本字段中獲取值??? 預先感謝。
根據我的理解,total_value文本字段不顯示任何內容。 您可以嘗試輸出選項的值並檢查它是否總是有值? 我建議你查看text_field_tag的文檔。 基本上,它接受三個這樣的變量:
text_field_tag(name, value = nil, options = {})
我正在使用getJSON方法....我覺得可以在這里使用。 希望以后的工作。
jQuery(document).ready(function()
{
jQuery(".customerid select").bind("change", function() {
var data = {
product_id: jQuery(this).val()
}
jQuery.getJSON(
"/controller_name/get_cust_bill",
data,
function(data){
var result = "";
res = parseFloat(a[1]);
jQuery('.price input').val(res);
});
});
});
調節器
def get_cust_bill
@cust_bill = CustomerBill.find_all_by_customer_id(params[:customer_id]).map{|p| [p.price]} if params[:customer_id]
respond_to do |format|
format.json { render json: @cust_bill }
end
end
所以不需要調用js。 你可以簡單地使用
<div class = "price"><%= f.input :price, :label => 'Price', :input_html => { :size => 20} %></div><br/>
祝一切順利 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.