简体   繁体   English

允许两个复选框/ RoR应用程序

[英]Allow Two checkboxes / RoR application

Hello I'm developing a form generator with RoR and I want allow only 2 checkboxes when I have a 'multiple choice' question. 您好,我正在使用RoR开发表单生成器,当我遇到“多项选择”问题时,我只希望允许2个复选框。 I wrote a JavaScript that works for this example : 我编写了一个适用于此示例的JavaScript:

My View example 我的视图示例

<div>
 <h4>
  Form 1
 </h4>
<form id="form_1">
  <input type="checkbox"  value="1">
  <input type="checkbox"  value="2">
  <input type="checkbox"  value="3">
</form>
</div>

<div>
<h4>
 Form 2
</h4>
 <form id="jj">
  <input type="checkbox" name="group" value="1">
  <input type="checkbox" name="group" value="2">
  <input type="checkbox" name="group" value="3">
</form>

My Javascript : 我的Java语言:

<script>

$(function() {
  $('form input[type=checkbox]').on('change', function(e) {
  var MyForm=this.parentNode.id;
  if ($('form[id='+MyForm+'] input[type=checkbox]:checked').length == 2) {
  $('form[id='+MyForm+'] input[type=checkbox]:not(:checked)').prop('disabled', 'disabled');
}else{
  $('form[id='+MyForm+'] input[type=checkbox]:not(:checked)').prop('disabled', false);
  }
 });

});

How can I adapt to to make it works with my Ruby code : 我如何适应使其与我的Ruby代码一起使用:

<%= form_for([@formulaire, @formulaire.polls.new]) do |f| %>
            <% @formulaire.questions.each_with_index do |question, i| %>
            <li>
                <div>

                <form id="form"> 
                <%= question.nom.html_safe %>
                <ul>
                   <% if question.typequestion == "choix_multiple"%>
                        <% question.answers.each_with_index do |answer, j| %>
                                <div>
                                <%  a= Answer.find_by_sql(["Select * from answers where id=?", answer.id]).as_json(only: [:question_id,:content]) %>
                                <%= check_box_tag :"nom_#{i}_#{j}", answer.id %> 
                                <%= f.label :"nom_#{answer.content}", answer.content%> <% answer.id%>
                                <br/>
                                </div>
                        <% end %>
                    <% elsif question.typequestion == "choix_simple"%>
                        (...)

        <%= f.submit "Valider les réponses", class:"pull-right btn btn-primary" %>
<% end %>

jQuery is not linked with the form. jQuery未与表单链接。 Simple solution is to add a class with each form and use that class in selector. 一个简单的解决方案是为每个表单添加一个类,然后在选择器中使用该类。

 $(document).ready(function() { $('.form input[type=checkbox]').on('change', function(e) { var MyForm=this.parentNode.id; if ($('form[id='+MyForm+'] input[type=checkbox]:checked').length == 2) { $('form[id='+MyForm+'] input[type=checkbox]:not(:checked)').prop('disabled', 'disabled'); }else{ $('form[id='+MyForm+'] input[type=checkbox]:not(:checked)').prop('disabled', false); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <div> <h4> Form 1 </h4> <form id="form_1" class="form"> <input type="checkbox" value="1"> <input type="checkbox" value="2"> <input type="checkbox" value="3"> </form> </div> <div> <h4> Form 2 </h4> <form id="jj" class="form"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> 

In your Ruby code 在您的Ruby代码中

<%= form_for([@formulaire, @formulaire.polls.new]) do |f| %>
            <% @formulaire.questions.each_with_index do |question, i| %>
            <li>
                <div>

                <form id="form" class="form"> 
                <%= question.nom.html_safe %>
                <ul>
                   <% if question.typequestion == "choix_multiple"%>
                        <% question.answers.each_with_index do |answer, j| %>
                                <div>
                                <%  a= Answer.find_by_sql(["Select * from answers where id=?", answer.id]).as_json(only: [:question_id,:content]) %>
                                <%= check_box_tag :"nom_#{i}_#{j}", answer.id %> 
                                <%= f.label :"nom_#{answer.content}", answer.content%> <% answer.id%>
                                <br/>
                                </div>
                        <% end %>
                    <% elsif question.typequestion == "choix_simple"%>
                        (...)

        <%= f.submit "Valider les réponses", class:"pull-right btn btn-primary" %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM