Jquery 使用ajax的动态表单
我的项目模型有一个表单,它是:make属性。根据选择的选项,它应该呈现共享相同的相应产品:item_make value。因此,如果您在第一个表单上选择“连帽衫”,则第二个表单上会显示可用的不同连帽衫。任何帮助都将不胜感激 引述 产品Jquery 使用ajax的动态表单,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我的项目模型有一个表单,它是:make属性。根据选择的选项,它应该呈现共享相同的相应产品:item_make value。因此,如果您在第一个表单上选择“连帽衫”,则第二个表单上会显示可用的不同连帽衫。任何帮助都将不胜感激 引述 产品 attr_accessible :name, :item_make has_many :items 项目 两个输入:(嵌套报价表单内部->项目表单) **编辑:**Application.js $(document).on('change', '.select_
attr_accessible :name, :item_make
has_many :items
项目
两个输入:(嵌套报价表单内部->项目表单)
**编辑:**Application.js
$(document).on('change', '.select_make', function() {
$.ajax({
type: 'GET',
url: "/quotes/update_products",
success: function(data) {
if (data.content == 'None')
{
$('.select_product').empty();
$('.select_product').append( $('<option>No make provided!</option>'));
}
else
$('.select_product').empty();
$('.selecT_product').append(); //unfinished
}
});
});
$(文档)。在('change','上。选择'u make',function(){
$.ajax({
键入:“GET”,
url:“/quotes/update\u产品”,
成功:功能(数据){
如果(data.content==“无”)
{
$('.select_product').empty();
$('.select_product')。追加($('No-make-provided!'));
}
其他的
$('.select_product').empty();
$('.selecT_product').append();//未完成
}
});
});
我在这里尝试过使用jquery的不同变体,但还没能让它正常工作
如果有人能告诉我做这件事所需的javascript,那就太棒了这样做怎么样:
def update_products
@make = params[:make]
@subproducts = Product.where(:item_make => @make).all
respond_with json: @subproducts.map{|x| [x.id, x.name]}
end
$.ajax({
type: 'GET',
url: "/quotes/update_products.json",
// Rest of the code....
});
您没有共享任何JS/Coffescript代码,请从该代码开始。此外,还不清楚模型之间是如何相互关联的,如果要使用嵌套属性的批量赋值,这一点很重要。@EdgarsJekabsons我添加了模型和javascript代码。我想做的是将子产品呈现或附加到.select\u产品类,或者学习更好的方法。如果这是url的正确语法,那么谢谢,但是我真正的问题是返回修改过的产品数组
<%= f.input :make, collection: clothing_types, label: 'Thread Type', remote: true,
url: url_for(controller: 'quotes', action: 'update_products'),
type: 'json', input_html: {class: "select_make"} %>
<%= f.input :product_id, input_html: {class: "select_product"} %>
def new
@quote = Quote.new
@quote.items.build
@item = Item.new
@products = Product.all
end
def update_products
@make = params[:make]
@subproducts = Product.where(:item_make => @make).all
render json: @subproducts.map{|x| [x.id, x.name]}
end
$(document).on('change', '.select_make', function() {
$.ajax({
type: 'GET',
url: "/quotes/update_products",
success: function(data) {
if (data.content == 'None')
{
$('.select_product').empty();
$('.select_product').append( $('<option>No make provided!</option>'));
}
else
$('.select_product').empty();
$('.selecT_product').append(); //unfinished
}
});
});
def update_products
@make = params[:make]
@subproducts = Product.where(:item_make => @make).all
respond_with json: @subproducts.map{|x| [x.id, x.name]}
end
$.ajax({
type: 'GET',
url: "/quotes/update_products.json",
// Rest of the code....
});