繁体   English   中英

从子输入中获取ID值,并在更改时将其推入Javascript中的数组

[英]Get id values from child inputs and push it to an array in Javascript on change

我试图将值放入数组[111、123、678等。。。]基本上,如果单击父检查值或子项的输入以及输出为array [],则与选中复选框相同它自己:我对JS有了很好的了解,但仍在努力。 我有一个jsfiddle http://jsfiddle.net/4ofugqvo/ ,这就是js的样子:

$(document).ready(function(){
    $('#sidebar').append(dropdown_theatre(events.level_0));

    function checkboxGroup() {
      $('#sidebarNav input[type="checkbox"]').on('change', function(){

              var checkboxes = $(this).parent().next('ul').find('input[type="checkbox"]');

              console.log(checkboxes);
        //[10101, 191919, 19191, 119191]

        if ($(this)[0].checked == true) {
          checkboxes.each(function(){
              $(this).prop('checked', true).attr('checked', 'checked');
          });
        } else {
          checkboxes.each(function(){
              $(this).prop('checked', false).removeAttr('checked');
              //console.log(this);
          });
        };

      });

    } checkboxGroup();

我叉了小提琴。 http://jsfiddle.net/0g1hk7mt/

我不太确定那是你的追求。 值的数组在on change事件中的警报处可用。

var arr = [];  
function checkboxGroup() {

    $('#sidebarNav input[type="checkbox"]').on('change', function(){
      arr = [];      
      var checkboxes = $(this).parent().next('ul').find('input[type="checkbox"]');

        //console.log(checkboxes);
      //[10101, 191919, 19191, 119191]

      if ($(this)[0].checked == true) {
        checkboxes.each(function(){
            arr.push($(this).attr("id"));
            $(this).prop('checked', true).attr('checked', 'checked');
        });
      } else {
        checkboxes.each(function(){
            $(this).prop('checked', false).removeAttr('checked');
            //console.log(this);
        });
      };
     alert(arr);
    });

  } 
checkboxGroup()

暂无
暂无

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

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