[英]Get id values from child inputs and push it to an array in Javascript on change
I am trying to get the values into an array [111, 123, 678, and so on....] basically if click parent check the values or children inputs and an output as array[], same way if checkbox is selected on its own: I am getting a good understanding of JS but still struggling with it. 我试图将值放入数组[111、123、678等。。。]基本上,如果单击父检查值或子项的输入以及输出为array [],则与选中复选框相同它自己:我对JS有了很好的了解,但仍在努力。 I have a jsfiddle http://jsfiddle.net/4ofugqvo/ and this is how the js looks like: 我有一个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();
I forked the fiddle. 我叉了小提琴。 http://jsfiddle.net/0g1hk7mt/ http://jsfiddle.net/0g1hk7mt/
I am not quite sure it's what you're after. 我不太确定那是你的追求。 The array of values is available at the alert in the on change event. 值的数组在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.