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

时间:2018-02-20 10:18:34

标签: javascript ruby-on-rails ruby

你好我正在开发一个带有RoR的表单生成器,当我有一个'多选'问题时,我想只允许2个复选框。我编写了一个适用于此示例的JavaScript:

我的视图示例

<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>

我的Javascript:

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

});

我如何适应使其适用于我的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 %>

1 个答案:

答案 0 :(得分:1)

jQuery没有与表单链接。简单的解决方案是为每个表单添加一个类,并在选择器中使用该类。

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

在您的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 %>