2011-08-09 8 views
0

私はjQueryを使用し、次のコードを持っていますが、それは部分的なものです。どのようにしてonchangeメソッドの特定のデータを取得し、その結果を次のタグに表示することができますか? (スパンタグ内)ajaxリクエストを介してデータを取得し、その結果を次のタグに表示する方法は?

注:この選択フィールドはオンザフライで生成されるため、N個のIDを持つことができます。

def retrieve_part_price 
    @price = Part.find(params[:id]) 
    respond_to do |format| 
    format.html { redirect_to(items_path)} 
    format.json { render @price } 
    end 
end 

そしてroutes.rbを上それを置く:

resources :items do 
    member do 
    get :update_part_price 
    end 
end 
また、私は(これが正しい場合、私は知らない)このアクションのためのメソッドを作成しました

<p class="fields"> 
    <%= f.collection_select(:part_id, @parts, :id, :title, { :prompt => true } , { :onchange => "?" }) %> 
    <span class='part_price'> _ _ _ _ </span> 
    <%= f.hidden_field :_destroy %> 
    <%= link_to_remove_fields "remove", f %> 
</p> 

答えて

0

解決済み。

私の部分:

<p class="fields"> 
<%= f.collection_select(:part_id, @parts, :id, :title, { :prompt => true } , { :onchange => "load_part_price_select(this.id, this.options[this.selectedIndex].value);" }) %> 
<%= f.label(:part_id, "Price: ") %> 
<span class="price"></span> 
<%= f.hidden_field :_destroy %> 
<%= link_to_remove_fields "remove", f %> 
</p> 

application.js

function load_part_price_select(id, value) { 
$('#' + id).live('change',function() { 
    $.ajax({ 
     url: '/parts/' + value + '/price', 
     type: 'get', 
     context: this, 
     dataType: 'script', 
     success: function(responseData) { 
     $(this).nextAll("span.price:first").html(responseData); 
     } 
    }); 
}); 
}; 

コントローラ:

def price 
    @price = Part.find(params[:id]) 
    respond_to do |format| 
    format.js { render :text => @price.price } 
    end 
end