简体   繁体   English

循环遍历html表并检查是否选中了复选框

[英]loop through html table and check if a checkbox is checked or not

What I'm trying to do here is validating HTML table. 我在这里尝试做的是验证HTML表。 I need at least one checkbox to be checked before proceeding. 在继续之前,我需要至少检查一个复选框。 It's doing what it's need to do, except when I don't check a checkbox. 它正在做它需要做的事情,除非我没有选中复选框。

It loops the alert "nothing found" by the count of data on the HTML table. 它通过HTML表上的数据计数循环警报“无法找到”。 How can I make it better? 我怎样才能让它变得更好? I need to loop inside the table to get the data on the same row with the checkbox checked. 我需要在表内循环,以便在选中复选框的同一行上获取数据。

JS JS

$('#dataTable').find('tr').each(function () {
    var row = $(this);
    if (row.find('input[type="checkbox"]').is(':checked') ) {
        alert "found"
        //im getting data here.
        });
    }else{
        alert "NOthing found"
    };
});

No need to loop through anything. 无需循环任何东西。 Just look for a checked checkbox inside dataTable with your selector. 只需使用您的选择器在dataTable中查找已选中的复选框。 If it exists, then your "true" condition is met. 如果存在,则满足您的“真实”条件。

if ($('#dataTable input[type="checkbox"]:checked').size()) {
    // Proceed
}

Now, if you want to do something with the data inside the "checked" rows, you could do something like this: 现在,如果您想对“已检查”行中的数据执行某些操作,您可以执行以下操作:

var checkedItems = $('#dataTable input[type="checkbox"]:checked').each(function() {
    // Do something with the row
    console.log($(this).parent('tr').find('.data-selector').val());
});
if (!checkedItems.size()) {
    // Nothing was checked
}

Likeso you will execute your code for every checked row, and if no row is checked, you will get to execute what you decide is apropriate, without much resources going to waste Likeso你将为每一个检查行执行你的代码,如果没有检查行,你将执行你认为合适的,没有太多资源浪费

  var is_table_empty = true;
 jQuery('#dataTable').find('input[type="checkbox"]:checked').each(function(){
    var row = jQuery(this).closest("tr");
    //do what needs to be done to an active row
    is_table_empty= false;
  });
  if(is_table_empty)
  {
     //do what you need to do if nothing is clicked
  }

try this same as your code. 尝试与您的代码相同。

var checkFound = false;

$('#dataTable').find('tr').each(function() {
  var row = ;
  if (!checkFound && $(this).find('input[type="checkbox"]').is(':checked')) {
    checkFound = true;
    return false;
  }
});

if (checkFound)
  alert('checked Found');
else
  alert('nothig checked');

In stead of alerting throughout the loop, why not use a flag and update it when something is found. 而不是整个循环中的警报,为什么不使用标志并在找到某些东西时更新它。 Based on the flag, (at the end - after the loop) just display the status!. 根据标志,(在结束 - 循环后)只显示状态! I hope the following will modification will help you: 我希望以下修改将帮助您:

var found = false;
$('#dataTable').find('tr').each(function () {   
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked') ) {
    found=true;
    //alert ("found"); 
    // Do your job here
    }
else{
     //alert "NOthing found"
     //found=false;
    };
});
if (!found){
    alert("Nothing Found");
}
else{
     alert("Found");// Or you can omit the else part
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM