2

我有一个嵌套表格,并使用 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
                });
            });

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

4

1 回答 1

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
       });
   });
});
于 2015-06-11T09:05:30.680 回答