简体   繁体   English

单选按钮和复选框不适用于jquery中的if / else

[英]Radiobuttons and checkboxes don't work with if/else in jquery

I have been working for two days now to get some code on radiobuttons and checkboxes working. 我已经工作了两天,以使单选按钮和复选框正常工作。 Still no luck though. 仍然没有运气。

To give a little context: based on the choice made through radiobuttons, there's a maximum allowed checkboxes to check. 给出一些背景信息:根据通过单选按钮做出的选择,有一个允许的最大复选框可以检查。 So for example if I choose 'One' from the radiobuttons, I'm allowed to select just one checkbox. 因此,例如,如果我从单选按钮中选择“一个”,则只允许选择一个复选框。

So the choices people get is 'None', 'One' and 'Two'. 因此,人们得到的选择是“无”,“一个”和“两个”。 If people choose none, the checkboxes stay hidden (this works). 如果人们都不选择,则复选框保持隐藏状态(此方法有效)。 If chosen 'One' or 'Two', the checkboxes appear (this also works). 如果选择“一个”或“两个”,则将出现复选框(这也有效)。

When I choose 'Two' I can get to select two checkboxes, and then when I choose 'One', I do get to choose just one checkbox. 当我选择“两个”时,我可以选择两个复选框,然后当我选择“一个”时,我确实只能选择一个复选框。 The problem however is when I switch from One to Two, I can always just choose one. 但是问题是,当我从一切换到二时,总是可以选择一个。

The HTML is as following: HTML如下:

<tr>
  <td class="label">
    <label for="splits">Split(s)</label>
  </td>
  <td class="value">
    <div id="picker_splits" class="radio-select select   swatch-control">
      <select id="splits" class="" name="attribute_splits" data-attribute_name="attribute_splits">
        <option value="">Een optie kiezen</option>
        <option value="Geen">Geen</option>
        <option value="Eén">Eén</option>
        <option value="Twee">Twee</option>
      </select>
      <ul id="radio_select_splits57ac19f756a99" class="" data-attribute_name="attribute_splits">
        <li>
          <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Geen" type="radio" data-value="Geen" value="Geen" />
          <label for="radio_splits57ac19f756a99_Geen">Geen</label>
        </li>
        <li>
          <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Eén" type="radio" data-value="Eén" value="Eén" />
          <label for="radio_splits57ac19f756a99_Eén">Eén</label>
        </li>
        <li>
          <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Twee" type="radio" data-value="Twee" value="Twee" />
          <label for="radio_splits57ac19f756a99_Twee">Twee</label>
        </li>
      </ul>
    </div><a class="reset_variations" href="#">Wissen</a>   
  </td>
</tr>
</tbody>
</table>

<div class="wccpf-fields-group-1">

  <table class="wccpf_fields_table " cellspacing="0">
    <tbody>
      <tr>
        <td class="wccpf_label">
          <label for="plaatsing_splits">Plaatsing split(s)</label>
        </td>
        <td class="wccpf_value">
          <ul class="wccpf-field-layout-horizontal">
            <li>
              <label>
                <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="linkerzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Linkerzijde</label>
            </li>
            <li>
              <label>
                <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="rechterzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Rechterzijde</label>
            </li>
            <li>
              <label>
                <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="achterzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Achterzijde</label>
            </li>

          </ul>
          <span class="wccpf-validation-message wccpf-is-valid-1">Wanneer je gekozen hebt voor een spit, mag dit veld niet leeg blijven</span>
        </td>
      </tr>

And here's the script: 这是脚本:

jQuery(document).ready(function($) {
  $("#picker_splits").change(function() {
    var kiesJeExtras = $("#picker_splits option:selected").val();
    var verborgenKeuze = $(".wccpf-fields-group-1");
    if (kiesJeExtras == "Eén") {
      verborgenKeuze.show('slow');
      $('input[type=checkbox]').on('change', function(e) {
        if ($('input[type=checkbox]:checked').length > 1) {
          $(this).prop('checked', false);
          alert("allowed only 1");
        }
      });
    } else if (kiesJeExtras == "Twee") {
      verborgenKeuze.show('slow');
      $("#picker_splits[value=Eén]").removeProp("selected");
      $(".wccpf-field[value=linkerzijde]").prop("checked", false);
      $(".wccpf-field[value=rechterzijde]").prop("checked", false);
      $(".wccpf-field[value=achterzijde]").prop("checked", false);
      $('input[type=checkbox]').on('change', function(e) {
        if ($('input[type=checkbox]:checked').length > 2) {
          $(this).prop('checked', false);
          alert("allowed only 2");
        }
      });
    } else {
      verborgenKeuze.hide('slow');
      $(".wccpf-field[value=linkerzijde]").prop("checked", false);
      $(".wccpf-field[value=rechterzijde]").prop("checked", false);
      $(".wccpf-field[value=achterzijde]").prop("checked", false);
      $('.wccpf-field[name=split_lengte]').val("0");
    }
  });
});

I hope someone can help me as I'm stuck for 2 two days now. 我希望有人可以帮助我,因为我现在停留了两天。 Can't figure out what the problem is. 无法找出问题所在。

Take a look at this snippet. 看看这个片段。 Instead of the if-cases I added a data-attribute that set the number of allowed checkboxes. 我添加了一个数据属性来设置允许的复选框数量,而不是if情况。

So first we get the number of allowed from data-value="x" . 因此,首先我们从data-value="x"获得允许的数量。 Then we check if we allow more or not. 然后,我们检查是否允许更多。

Note that I haven't implemented it in the dropdown. 请注意,我尚未在下拉菜单中实现它。

 $('.wccpf-field-layout-horizontal input').change(function(){ var noOfSelected = $('.wccpf-field-layout-horizontal input:checked').length; // Get the allowed number based on the checked radio. var noOfAllowed = $('#picker_splits input:checked').data('value'); if(noOfSelected > noOfAllowed){ alert('You have not checked ' + noOfSelected + '. You are only allowed to check ' + noOfAllowed); $(this).prop('checked', false); } }); $('#picker_splits input').change(function(){ if($(this).data('value') == 0) { // Uncheck all. $('.wccpf-field-layout-horizontal input:checked').prop('checked',false); // Possibly hide? //$('.wccpf-field-layout-horizontal input:checked').hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td class="label"> <label for="splits">Split(s)</label> </td> <td class="value"> <div id="picker_splits" class="radio-select select swatch-control"> <select id="splits" class="" name="attribute_splits" data-attribute_name="attribute_splits"> <option value="">Een optie kiezen</option> <option value="Geen">Geen</option> <option value="Eén">Eén</option> <option value="Twee">Twee</option> </select> <ul id="radio_select_splits57ac19f756a99" class="" data-attribute_name="attribute_splits"> <li> <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Geen" type="radio" data-value="0" value="Geen" /> <label for="radio_splits57ac19f756a99_Geen">Geen</label> </li> <li> <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Eén" type="radio" data-value="1" value="Eén" /> <label for="radio_splits57ac19f756a99_Eén">Eén</label> </li> <li> <input class="radio-option" name="attribute_splits_57ac19f756a3b" id="radio_splits57ac19f756a99_Twee" type="radio" data-value="2" value="Twee" /> <label for="radio_splits57ac19f756a99_Twee">Twee</label> </li> </ul> </div><a class="reset_variations" href="#">Wissen</a> </td> </tr> </tbody> </table> <div class="wccpf-fields-group-1"> <table class="wccpf_fields_table " cellspacing="0"> <tbody> <tr> <td class="wccpf_label"> <label for="plaatsing_splits">Plaatsing split(s)</label> </td> <td class="wccpf_value"> <ul class="wccpf-field-layout-horizontal"> <li> <label> <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="linkerzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Linkerzijde</label> </li> <li> <label> <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="rechterzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Rechterzijde</label> </li> <li> <label> <input type="checkbox" class="wccpf-field" name="plaatsing_splits[]" value="achterzijde" wccpf-type="checkbox" wccpf-pattern="mandatory" wccpf-mandatory="no" />Achterzijde</label> </li> </ul> <span class="wccpf-validation-message wccpf-is-valid-1">Wanneer je gekozen hebt voor een spit, mag dit veld niet leeg blijven</span> </td> </tr> </table> 

Change $("#picker_splits") to $("#splits") . $("#picker_splits")更改$("#picker_splits") $("#splits")

$("#splits").change(function() {
   var kiesJeExtras = $("#splits option:selected").val();
   ...

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

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