在选择更改时部分渲染轨道 [英] Render rails partial on select change

查看:68
本文介绍了在选择更改时部分渲染轨道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像的嵌套形式:

I have a nested_form that looks like:

<% form_for @store, do |f| %>
   <%= f.fields_for :products do |builder| %>
      <%= builder.select :typeis, %w[test test1 test2], {}, class: "select_product" %>
      <div class='productHere'></div>
   <% end %>
<% end %>

基本上,我想做的是选择onChange,取选择和渲染的值部分喜欢:

Basically, what I want to do is onChange of the select, take the value of the select and render a partial like:

<%= f.hidden_field :name, value: "blah" %>

使用该值。因此,如果select的值为 test1 ,我想渲染_test1.html.erb。

using the value. So if the value of the select is test1, I want to render _test1.html.erb.

我尝试过:

<script type='text/javascript'>
  $(function (){
     $(".select_product").on("change", function (){
        alert(<%= render 'product_form/test', f:builder %>);
     });
  });
</script>

但这不起作用。我做错了什么?

But this doesn't work. What am I doing wrong?

感谢您的帮助!

推荐答案

当您动态创建选择下拉列表时,您需要使用以下代码绑定更改事件:

As you are creating select dropdown dynamically, then you need to bind change event using below code:

<script type='text/javascript'>
  $(function (){
     $(document).on("change",".selectProduct", function (){
        var selectValue = $(this).val();
        alert(<%= render 'product_form/test', f:builder %>);
     });
  });
</script>

这篇关于在选择更改时部分渲染轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆