简体   繁体   English

从一个复选框中选择多个值并显示一个元素,该元素对于复选框中的每个值都不同

[英]Selecting more than one value from a check box and showing an element that is different for each value from the check box

So I have this html code of a check box with multiple selections.所以我有一个带有多个选择的复选框的 html 代码。

If I select 1, I want to show element1, If I select 2, I want to show element2, If I select 1 and 2 together, show 1 and 2 together.如果我 select 1,我想显示 element1,如果我 select 2,我想显示 element2,如果我 select 1 和 2 一起显示。

I'm trying to figure it out with this code, but it's just not working!我试图用这段代码来解决这个问题,但它只是不起作用!

 $(document).ready(function() { $('#nearbyy').bind('change', function(e) { if ($('#nearbyy').val() == '1') { $("#Airport").show(); $("#Garden").hide(); } else if ($('#nearbyy').val() == '2') { $("#Airport").hide(); $("#Garden").show(); } else if ($('#nearbyy').val() == '1' || $('#nearbyy').val() == '2') { $("#Airport").show(); $("#Garden").show(); } else { $("#Airport").hide(); $("#Garden").hide(); } }).trigger('change'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>List of Stores which completed the survey so far</label> <select multiple="" for="postage" id="nearbyy" class="form-control"> <option value="">Select...</option> <option value="1" id ="1">Airport</option> <option value="2" id ="2">Garden</option> </select> <br /> <div id="Airport" class="form-control"> Element1 </div> <br /> <div id="Garden" class="form-control"> Element2 <br /> <br /> </div>

Kind regards亲切的问候

Hold Ctrl in windows, Cmd in mac for multi-select.在 mac 中按住Ctrl在 windows、 Cmd中进行多选。

For Multi Select the value will be in a Array format.对于Multi Select ,该值将采用数组格式。 So you can't match it as a single value.因此,您不能将其作为单个值进行匹配。 And set the ID to your element same as the option .并将ID设置为与option相同的元素。 So you can directly access the element without IF Condition .因此,您可以在没有IF 条件的情况下直接访问元素。 This is called Code Optimisation这称为代码优化

 $("#nearbyy").change(function(e){ const value = $(e.target).val(); // value list $('.element').hide(); // hide all elements first // loop through array value.forEach(x => { try { $('#'+x).show(); // show only selected elements } catch(ex) { //not select anything or element not found } }) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>List of Stores which completed the survey so far</label> <select multiple="" for="postage" id="nearbyy" class="form-control"> <option value="">Select...</option> <option value="1">Airport</option> <option value="2">Garden</option> </select> <br /> <div id="1" class="form-control element" style='display:none'> Element1 </div> <br /> <div id="2" class="form-control element" style='display:none'> Element2 <br /> <br /> </div>

$('#nearbyy').val() returns an array, so you need to check if your values are in that array. $('#nearbyy').val()返回一个数组,因此您需要检查您的值是否在该数组中。

Here is a solution close to your original code.这是一个接近原始代码的解决方案。 Notice, that you also need to check for occurence of both values at first if you use an else if structure.请注意,如果您使用else if结构,您还需要首先检查这两个值的出现。

jquery's inArray() method returns -1 if the value is not in the array.如果值不在数组中,jquery 的inArray()方法将返回-1

 $(document).ready(function() { $('#nearbyy').bind('change', function(e) { const values = $('#nearbyy').val(); if (jQuery.inArray("1", values) > -1 && jQuery.inArray("2", values) > -1) { $("#Airport").show(); $("#Garden").show(); } else if (jQuery.inArray("1", values) > -1) { $("#Airport").show(); $("#Garden").hide(); } else if (jQuery.inArray("2", values) > -1) { $("#Airport").hide(); $("#Garden").show(); } else { $("#Airport").hide(); $("#Garden").hide(); } }).trigger('change'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>List of Stores which completed the survey so far</label> <select multiple for="postage" id="nearbyy" class="form-control"> <option value="">Select...</option> <option value="1" id ="1">Airport</option> <option value="2" id ="2">Garden</option> </select> <br /> <div id="Airport" class="form-control"> Element1 </div> <br /> <div id="Garden" class="form-control"> Element2 <br /> <br /> </div>

Try .css() instead of .show() and .hide()尝试.css()而不是.show()和 .hide( .hide()

<script>
$(document).ready(function() {
  $('#nearbyy').bind('change', function(e) {
    if ($('#nearbyy').val() == '1') {
      $("#Airport").css("display", "unset");
      $("#Garden").css("display", "none");
    } else if ($('#nearbyy').val() == '2') {
      $("#Airport").css("display", "none");
      $("#Garden").css("display", "unset");
    } else if ($('#nearbyy').val() == '1' .val() == '2') {
      $("#Airport").css("display", "unset");
      $("#Garden").css("display", "unset");
    } else {
      $("#Airport").css("display", "none");
      $("#Garden").css("display", "none");
    }
  }).trigger('change');
});
</script>

Read more: https://api.jquery.com/css/#css2阅读更多: https://api.jquery.com/css/#css2

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

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