gpt4 book ai didi

jquery - nested_form gem 和 jquery 的 Rails 问题

转载 作者:行者123 更新时间:2023-11-28 01:29:39 24 4
gpt4 key购买 nike

我有一个嵌套形式并使用 ryan bates 的 nested_form gem,其中我在该领域使用 raty stars,形式给出为

<%= 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
});
});

问题是当我点击添加一个问题之前的星级评级为空时,我知道为什么会发生这种情况,因为我再次将 raty 函数传递给所有星级问题请告诉我如何添加保留之前星级的问题

最佳答案

试试下面的代码-

$(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
});
});
});

关于jquery - nested_form gem 和 jquery 的 Rails 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30774622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com