簡體   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