jQuery顶级包选择器

时间:2018-11-06 23:25:05

标签: javascript jquery

我有多个软件包可供选择。

<div class="image-grid-item" data-search="select">
<input name="pack1" type="checkbox" style="display: none;">
</div>
<div class="image-grid-item" data-search="select">
<input name="pack1" type="checkbox" style="display: none;">
</div>
<div class="image-grid-item" data-search="select">
<input name="pack2" type="checkbox" style="display: none;">
</div>
<div class="image-grid-item" data-search="select">
<input name="pack2" type="checkbox" style="display: none;">
</div>
<div class="image-grid-item" data-search="select">
<input name="pack3" type="checkbox" style="display: none;">
</div>
<div class="image-grid-item" data-search="select">
<input name="pack3" type="checkbox" style="display: none;">
</div>

与此相同,我有很多属于不同包装的物品。

这是我的jQuery

  jQuery(".image-grid-item").click(function(){
     jQuery('input', this).prop("checked", true);
     //alert("test");

     if(jQuery('input[name="pack1"]').prop("checked") == true){        
        jQuery('.package1').show();
      }
      else if(jQuery('input[name="pack2"]').prop("checked") == true){        
        jQuery('.package2').show();
      }
      else if(jQuery('input[name="pack3"]').prop("checked") == true){        
        jQuery('.package3').show();
      }

      else{
        // jQuery('input',this).prop('checked', true);

      }
  });

这是我的div,显示正确的包裹。

<div class="package1" style="display: none;">
package1
</div>
<div class="package2" style="display: none;">
package2
</div>
<div class="package3" style="display: none;">
package3
</div>

如果我选择package1,它显示正确的软件包,但是当我选择package1和package3时,显示两个软件包,但是我只想显示package3。 或者,当我选择package1和package2时,只需要显示较大的包就意味着package2。

1 个答案:

答案 0 :(得分:1)

这是一个非常简单的逻辑,需要完成。请从此示例中学习,并尝试思考问题或尝试解决问题,然后再在此处发布...这将有很长的路要走。

请参阅代码注释,以了解其工作原理。

//When we click on the div

$('.image-grid-item').on('click', function() {
  var selected = [];

  //Allow a package to be selected & deslected
  if ($(this).children().prop('checked') == true) {
    $(this).children().prop('checked', false)
  } else {
    $(this).children().prop('checked', true)
  }


  //For every div on the pack
  $('.image-grid-item').each(function(index, element) {

    //Check if the input inside of it is checked
    if ($(element).children().prop('checked') == true) {

      // if it is checked then store the package number in the array
      selected.push($(element).data('package'));
    }
  });


  var packageSizes = [];
  //Loop through the array of selected packages we made earlier 
  $.each(selected, function(index, value) {

    //Then loop through each package div on the page
    $('.package').each(function(index, element) {
    
      /*If the current item in the loop has the same data attribute value
      as the div we are also currently looping, then... 
      */
      if ($(element).data('package') == value) {
      
        //Store the package size in the package array
        packageSizes.push($(element).data('size'))
      }
      
      //(Or in other words, if we check a box, store the package size in an array)
    });
  })


  //Now we have the sizes of w/e packages are selected, get the largest size
  var largestPackage = Math.max.apply(Math, packageSizes);

  //Hide all packages initiallly
  $('.package').hide();
  
  //Loop through all package divs
  $('.package').each(function(index, element) {
  
    //If the package's size is equal to the largest package we selected then...
    if ($(element).data('size') == largestPackage) {
    
      //Display only that largest package
      $(element).show();
    }
  });
})
.image-grid-item {
  border: 1px solid red;
  width: 80px;
  height: 15px;
  padding: 5px;
  display: inline-block;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="image-grid-item" data-package="1" data-search="select">
  <label>size 5</label>
  <input name="pack1" type="checkbox">
</div>
<div class="image-grid-item" data-package="2" data-search="select">
  <label>size 10</label>
  <input name="pack2" type="checkbox">
</div>
<div class="image-grid-item" data-package="3" data-search="select">
  <label>size 15</label>
  <input name="pack3" type="checkbox">
</div>

<div class="package" data-package="1" data-size="5" style="display: none;">
  package1
</div>
<div class="package" data-package="2" data-size="10" style="display: none;">
  package2
</div>
<div class="package" data-package="3" data-size="15" style="display: none;">
  package3
</div>

如果您认为此答案已解决您的问题,请点击投票箭头旁边的对勾按钮。如果您对此答案还有其他疑问/疑问,请在下面评论。

值得注意的是,这种方法可以减少并提高效率,但是我发现这样写使它更易于遵循和更具可读性。

编码愉快!