【问题标题】:Rails Issue with nested_form gem and jquery嵌套表单 gem 和 jquery 的 Rails 问题
【发布时间】:2015-08-26 18:49:40
【问题描述】:

我有一个嵌套表格,并使用了 ryan bates 的nested_form gem,我在该领域使用了老鼠星,表格为

<%= f.fields_for :round_questions do |question| %>
     <%= question.label :question %>
     <%= question.text_field :question %>

     <div class="star-questions" > </div>
     <%= question.text_field :answer %>

<% end %>

<%= f.link_to_add "Add a Question", :round_questions,
  :class=> 'btn waves-effect waves-light btn-medium custom_btn_gray',:id => "add-fields" %>

并且 javascript 被给出为

$(document).ready(function() {
    $('.star-questions').raty({

        targetType : 'score',
        targetKeep : true
});

            $(document).on('click', '#add-fields', function(){

                $('.star-questions').raty({
                    targetType : 'score',
                    targetKeep : true
                });
            });

问题是,当我点击添加问题时,之前的星级评分为空,我知道为什么会这样,因为我再次将评分函数传递给所有星级问题 请告诉我如何添加保留以前星级的问题

【问题讨论】:

  • 这是一个解决方案吗?我不明白
  • @nishantvarshney 你确定这是一个有效的 JS 吗?
  • @hawk 我已经编辑了 JS 请仔细阅读

标签: jquery html ruby-on-rails-4 nested-form-for raty


【解决方案1】:

试试下面的代码-

$(function(){
  $('.star-questions').raty({
        targetType : 'score',
        targetKeep : true
      });

     $(document).on('nested:fieldAdded:round_questions', function(event){
        event.field.find('.star-questions').raty({
          targetType : 'score',
          targetKeep : true
       });
   });
});

【讨论】:

  • 我已经编辑了您的答案,您忘记了要输入的内容,但是这个逻辑有效,非常感谢!!
猜你喜欢
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多