简体   繁体   English

Jquery输入:选中show和hide div函数

[英]Jquery input :checked show and hide div function

I am trying to create a simple JQuery function. 我正在尝试创建一个简单的JQuery函数。 This function has three check boxes and a compare button. 此功能有三个复选框和一个比较按钮。 Example can be Seen here 可以在这里看到示例

The objective of this function is when the user clicks on checkboxes 1 and 2 it compares the content of hidden containers one and two. 此功能的目标是当用户点击复选框1和2时,它会比较隐藏容器1和2的内容。 When users clicks on checkboxes 1 and 3 it compares the content of hidden containers one and three. 当用户点击复选框1和3时,它会比较隐藏容器1和3的内容。

I can't get this function get this working. 我无法得到这个功能让这个工作。 I added a snippet of code below 我在下面添加了一段代码

 $(document).ready(function() { var $checkboxes = $('[name^="check"]'); var $target = $('#target'); var $button = $('.compare'); $button.on('click', function() { $('.data').hide(); $('.checkBox:checked').each(function() { var div = $(this).attr('name'); $('.compare-block').find('.'+div).show(); }) }) }) 

I changed a bit your snippet. 我改变了一下你的片段。 You are using only the length, but if you want to compare 1 and 3 or X and Y, you need to know wich ones are selected. 您只使用长度,但如果要比较1和3或X和Y,则需要知道选择了哪个。

The changed I did, first you check if there is more than one to compare, and then, you see wich ones. 我做了改变,首先你检查是否有不止一个比较,然后,你看到了哪些。 I edited a bit the HTML to add some ids 我编辑了一些HTML来添加一些ID

$(document).ready(function() {
  var $button  = $('.compare');

  $button.on('click', function() {
    var checkedLength = $('[name^="check"]:checked').length;
    $('#target .compare-block').hide();
    if (checkedLength > 1 ) {
      $( '[name^="check"]:checked' ).each(function() {
        //$(this).val() I use value, but you can use another thing
        $('#container'+$(this).val()).show();
      });
    }
  })
}); 

https://jsfiddle.net/t95kgzvx/4/ https://jsfiddle.net/t95kgzvx/4/

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

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