[英]Allow Two checkboxes / RoR application
您好,我正在使用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>
<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 %>
jQuery未與表單鏈接。 一個簡單的解決方案是為每個表單添加一個類,然后在選擇器中使用該類。
$(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>
在您的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.