admin管理员组文章数量:1130349
I'm trying to get values of a table with jQuery. Every table row has a checkbox at the begining. How can I copy data of each row, but only if it's checked. All checkboxes and rows has the same class. Now I'm using this code:
var table = $("table");
var allRows = [];
table.find('tr').each(function (i, el) {
if($('input:checkbox:checked').length > 0){
var $tds = $(this).find('td'),
name = $tds.eq(2).text(),
surname = $tds.eq(4).text();
allRows.push(surname+";"+name+";"+"\n");
}
});
allRows = allRows.join("");
But it's getting all the lines. How can I do this only for lines which are checked?
I'm trying to get values of a table with jQuery. Every table row has a checkbox at the begining. How can I copy data of each row, but only if it's checked. All checkboxes and rows has the same class. Now I'm using this code:
var table = $("table");
var allRows = [];
table.find('tr').each(function (i, el) {
if($('input:checkbox:checked').length > 0){
var $tds = $(this).find('td'),
name = $tds.eq(2).text(),
surname = $tds.eq(4).text();
allRows.push(surname+";"+name+";"+"\n");
}
});
allRows = allRows.join("");
But it's getting all the lines. How can I do this only for lines which are checked?
Share Improve this question asked May 6, 2017 at 12:32 passwdpasswd 3,3533 gold badges15 silver badges25 bronze badges 1- Do you have jsfiddle of this? – Yuvraj Commented May 6, 2017 at 12:34
2 Answers
Reset to default 9$('.theClass:checkbox:checked') will give you all the checked checkboxes with the class theClass.
See also Get all checked checkboxes
As of now you are checking any checkbox is checked. the condition needs to be changed.
You need to check the :checked state of the chechbox in current row while iterating.
if($(this).find(':checkbox:checked').length > 0){
//your existing code
}
I'm trying to get values of a table with jQuery. Every table row has a checkbox at the begining. How can I copy data of each row, but only if it's checked. All checkboxes and rows has the same class. Now I'm using this code:
var table = $("table");
var allRows = [];
table.find('tr').each(function (i, el) {
if($('input:checkbox:checked').length > 0){
var $tds = $(this).find('td'),
name = $tds.eq(2).text(),
surname = $tds.eq(4).text();
allRows.push(surname+";"+name+";"+"\n");
}
});
allRows = allRows.join("");
But it's getting all the lines. How can I do this only for lines which are checked?
I'm trying to get values of a table with jQuery. Every table row has a checkbox at the begining. How can I copy data of each row, but only if it's checked. All checkboxes and rows has the same class. Now I'm using this code:
var table = $("table");
var allRows = [];
table.find('tr').each(function (i, el) {
if($('input:checkbox:checked').length > 0){
var $tds = $(this).find('td'),
name = $tds.eq(2).text(),
surname = $tds.eq(4).text();
allRows.push(surname+";"+name+";"+"\n");
}
});
allRows = allRows.join("");
But it's getting all the lines. How can I do this only for lines which are checked?
Share Improve this question asked May 6, 2017 at 12:32 passwdpasswd 3,3533 gold badges15 silver badges25 bronze badges 1- Do you have jsfiddle of this? – Yuvraj Commented May 6, 2017 at 12:34
2 Answers
Reset to default 9$('.theClass:checkbox:checked') will give you all the checked checkboxes with the class theClass.
See also Get all checked checkboxes
As of now you are checking any checkbox is checked. the condition needs to be changed.
You need to check the :checked state of the chechbox in current row while iterating.
if($(this).find(':checkbox:checked').length > 0){
//your existing code
}
本文标签: JavaScriptJQuerycheck if checkbox is checked with the same classStack Overflow
版权声明:本文标题:javascript - jQuery, check if checkbox is checked with the same class - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1741057928a1821187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论