![](/img/trans.png)
[英]Use Javascript/Jquery to iterate through table rows and checkboxes and add attribute of checkbox to array if checked
[英]Javascript/JQuery iterate through table rows and cells and output an attr of checked checkboxes to console
我有以下代码,应该遍历每个表行并转储在较早的javascript段中声明的数组。 然后,如果选中此复选框,并且其attr为“ changed = yes”,则应将其推入数组,并在控制台中输出该值以及应作为变量输出的“ path”属性,每次该功能找到一个已选中并更改的新复选框时,都可以将其覆盖。 那么我的代码有什么问题呢? 这些功能包含在用户单击表单上的“提交”时调用的功能中。
JsFiddle: http : //jsfiddle.net/hU89p/392/
$('#myTable1 tr').each(function(){
myArray = [];
$.each($("input[type='checkbox']:checked").closest("td").siblings("td"),
function () {
if($(this).data("changed") == 'yes'){
myArray.push($(this).attr('checkboxtype'));
filepath = $(this).attr('path');
console.log(myArray);
console.log(filepath);
}
});
});
这是工作小提琴 :
把事情简单化 :
$('#myTable1 tr').each(function() {
var columns = $(this).find('td');
columns.each(function() {
var box = $(this).find('input:checkbox');
if(box.is(":checked") && box.attr("changed") == 'yes')
{
myArray.push(box.attr('checkboxtype'));
filepath = box.attr('path');
}
});
});
console.log(myArray);
});
您应该使用$("input[type='checkbox']:checked").closest("td").siblings("td").each()
。
查看$().each()
和$.each()
之间的区别。
try this :-
$('#myTable1 tr').each(function () {
myArray = [];
$(this).find("td input:checkbox").each(function () {
if ($(this).is(":checked") && $(this).attr("changed") == 'yes') {
myArray.push($(this).attr('checkboxtype'));
filepath = $(this).attr('path');
console.log(myArray);
console.log(filepath);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.