1

こんにちは皆さん、ここで動的選択を試みています。顧客を選択するとすぐに、請求書の合計値がテキスト フィールド タグに表示されます。

景色

  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ので、テキストフィールドで値を取得する方法を教えてもらえますか??? 少し早いですがお礼を。

4

2 に答える 2

2

私が理解していることから、total_value テキスト フィールドには何も表示されません。オプションの値を出力して、常に値があるかどうかを確認していただけますか? text_field_tagのドキュメントを確認することをお勧めします。基本的に、次のような 3 つの変数を受け入れます。

text_field_tag(name, value = nil, options = {})
于 2012-08-07T10:08:57.753 に答える
1

私は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 を呼び出す必要はありません。あなたが簡単に持つことができるerbパーシャル

<div class = "price"><%= f.input :price, :label => 'Price', :input_html => { :size => 20} %></div><br/>

ではごきげんよう :)

于 2012-09-13T09:29:56.587 に答える