循环浏览html表格并获取选中的复选框(JQuery) [英] Loop over html table and get checked checkboxes (JQuery)

查看:101
本文介绍了循环浏览html表格并获取选中的复选框(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')// ...
});

让我解释一下选择器的作用:
input [ type =checkbox] 表示这将匹配每个< input /> 与类型属性 type 等于复选框
之后::checked 将匹配所有选中的复选框。 / p>

您可以在这些复选框中循环:

  $('#保存')。click(function(){
$('#mytable')。find('input [type =checkbox]:checked')。each(function(){
//这是当前复选框
});
});

这里是演示 JSFiddle






下面是一个演示,它可以准确地解决您的问题http://jsfiddle.net/DuE8K/1/

 <$ 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 have an HTML table with a checkbox in each row.
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);
});

This prints in the console the following:

[prevObject: jQuery.fn.jQuery.init[1], context: tr, selector: "input:checked", constructor: function, init: function…]

per row regardless of whether any checkbox is checked.

Update
Same issue with:

$('#mytable tr').each(function (i, row) {                                                                                                 
   var $actualrow = $(row);
    $checkbox = $actualrow.find(':checkbox:checked');
    console.log($checkbox);  
});

解决方案

Use this instead:

$('#save').click(function () {
    $('#mytable').find('input[type="checkbox"]:checked') //...
});

Let me explain you what the selector does: 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.

You can loop over these checkboxes with:

$('#save').click(function () {
    $('#mytable').find('input[type="checkbox"]:checked').each(function () {
       //this is the current checkbox
    });
});

Here is demo in JSFiddle.


And here is a demo which solves exactly your problem http://jsfiddle.net/DuE8K/1/.

$('#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!');
        }
    });
});

这篇关于循环浏览html表格并获取选中的复选框(JQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆