循环浏览html表格并获取选中的复选框(JQuery) [英] Loop over html table and get checked checkboxes (JQuery)
问题描述
我在每一行都有一个带有复选框的HTML表格。
我想遍历表格并查看是否有任何复选框被选中。
以下是不工作:
$(#save)。click(function(){
$('#mytable (行,行){
var $ actualrow = $(row);
checkbox = $ actualrow.find('input:checked');
console。 log($ checkbox);
});
[prevObject:jQuery.fn.jQuery.init [1],context:tr,selector:input:checked,构造函数:function ,init:function ...]
每行,不管是否选中任何复选框。
$ b $
$('#$ c $> mytable tr')。each(function(i,row){
。) var $ actualrow = $(row);
$ checkbox = $ actualrow.find(':checkbox:checked');
console.log($ checkbox);
});
b
$('#save')。click(function(){
$('#mytable')。find('input [type = checkbox]:checked')// ...
});
让我解释一下选择器的作用: 您可以在这些复选框中循环: 这里是演示 JSFiddle 。 下面是一个演示,它可以准确地解决您的问题http://jsfiddle.net/DuE8K/1/ 。 I have an HTML table with a checkbox in each row. This prints in the console the following: per row regardless of whether any checkbox is checked. Update
Use this instead: Let me explain you what the selector does:
You can loop over these checkboxes with: Here is demo in JSFiddle. And here is a demo which solves exactly your problem http://jsfiddle.net/DuE8K/1/.
这篇关于循环浏览html表格并获取选中的复选框(JQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
input [ type =checkbox]
表示这将匹配每个< input />
与类型属性 type
等于
复选框
之后::checked
将匹配所有选中的复选框。 / p>
$('#保存')。click(function(){
$('#mytable')。find('input [type =checkbox]:checked')。each(function(){
//这是当前复选框
});
});
<$ c $ ($)$('#save')。click(function(){
$('#mytable')。find('tr')。each(function(){
var row = $( (':checked')&&
row.find('textarea')。val);
if(row.find('input [type =checkbox]')。 ().length< = 0){
alert('您必须填写文本区!');
}
});
});
I want to loop over the table and see if there are any checkboxes that are checked.
The following does not work: $("#save").click( function() {
$('#mytable tr').each(function (i, row) {
var $actualrow = $(row);
checkbox = $actualrow.find('input:checked');
console.log($checkbox);
});
[prevObject: jQuery.fn.jQuery.init[1], context: tr, selector: "input:checked", constructor: function, init: function…]
Same issue with: $('#mytable tr').each(function (i, row) {
var $actualrow = $(row);
$checkbox = $actualrow.find(':checkbox:checked');
console.log($checkbox);
});
$('#save').click(function () {
$('#mytable').find('input[type="checkbox"]:checked') //...
});
input[type="checkbox"]
means that this will match each <input />
with type attribute type
equals to checkbox
After that: :checked
will match all checked checkboxes.$('#save').click(function () {
$('#mytable').find('input[type="checkbox"]:checked').each(function () {
//this is the current checkbox
});
});
$('#save').click(function () {
$('#mytable').find('tr').each(function () {
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked') &&
row.find('textarea').val().length <= 0) {
alert('You must fill the text area!');
}
});
});