大家好,我正在尝试在这里尝试动态选择。一旦我选择了客户,他在账单中的总值(value)就会出现并显示在文本字段标签中。
景色
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 %>
在 Controller 中
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 = {})
关于javascript - 在 ruby on rails 的文本字段中动态显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11839947/