Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未定义的局部变量或方法'f';对于,使用jquery进行部分渲染时_Javascript_Jquery_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Javascript 未定义的局部变量或方法'f';对于,使用jquery进行部分渲染时

Javascript 未定义的局部变量或方法'f';对于,使用jquery进行部分渲染时,javascript,jquery,ruby-on-rails,ruby,ruby-on-rails-5,Javascript,Jquery,Ruby On Rails,Ruby,Ruby On Rails 5,单击按钮时。。必须显示部分内容 $('#info_fields').html( '<%= render partial: "investor_info_fields", locals: { f: f } %>'); $('#信息字段').html(''; 部分形式 <%= f.fields_for :investor_infos do |f| %> <div class="nested-fields">

单击按钮时。。必须显示部分内容

$('#info_fields').html( '<%= render partial: "investor_info_fields", locals: { f: f } %>');
$('#信息字段').html('';
部分形式

<%= f.fields_for :investor_infos do |f| %>
            <div class="nested-fields">
              <%= f.label :name %> <br>
              <%= f.text_field :name, label: "invested companies", class: "input-md form-control mb-20" %>

               <%= f.label :investment_size_id, "Investment size" %> <strong> *</strong><br>
              <%= f.collection_select :investment_size_id, InvestmentSize.all, :id, :size, {}, {class: "input-md form-control mb-20 startup_info_required"} %><br>


              <%= f.label :investment_time_id, "Investment time" %> <strong> *</strong><br>
              <%= f.collection_select :investment_time_id, InvestmentTime.all, :id, :time, {}, {class: "input-md form-control mb-20 startup_info_required"} %><br>
            </div>
          <% end %>


*

*

Jquery:

$(".my-class").click(function() {
               var whut=$(this).val();
               ($(this).val() == 4) {

               $('#info_fields').html( '<%= render partial: "investor_info_fields", locals: { f: f } %>');

        }
          });
$(“.my class”)。单击(函数(){
var whut=$(this.val();
($(this.val()==4){
$('#信息字段').html('');
}
});

图:

在这里,您必须进行以下两项更改:

  • 正如我在评论中提到的,如果您已经编写了脚本 在form_标签中,它将不起作用。所以把你的脚本放在表单标签里

  • 替换以下行:

    $(“#信息字段”).html(“”)

    与:

    $(“#信息字段”).html(“”)


  • 在这里,我在
    render
    关键字前面添加了一个
    j
    (又称escape\u javascript),如果部分中有一些ruby代码,而不是简单的html代码,它可以帮助您从javascript中呈现部分内容。

    您在哪里编写了这个脚本?如果它在form_标签之外,它将不起作用。你需要把它写在form_tag.kkk。。我是在main表单标记之外编写的。我想在任何ajax请求中都会有这样的内容。这个f应该是form对象。并且应该在表单中。获取。。。我只写了表单标签。添加了jquery代码。。在里面quetion@GaneshRaju这不起作用,因为我可以看到
    script
    块中没有
    f
    局部变量。你能举例说明你想做什么吗?谢谢。。现在它开始工作了。我忘了放if语句。但正在渲染重复字段。方法第一次是完美的,但是,如果第二次单击,相同的字段将呈现两次…..为此,请附上一个带有您的问题的屏幕截图?屏幕截图。。附上两件东西。第一:图片中没有显示在帖子中。我必须通过查看编辑来访问它。第二:请再解释一下,因为图像没有重复的字段。我还添加了图像,但没有显示。谢谢你@Vishal。问题解决了。