javascript jquery只获取tr的第一个复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18887696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:34:22  来源:igfitidea点击:

jquery get only first checkbox of a tr

javascriptjqueryhtmlcheckbox

提问by vanessen

I want to check if the first checkbox in a tr is selected not all the checkbox that is found in this tr.

我想检查 tr 中的第一个复选框是否被选中,而不是在此 tr 中找到的所有复选框。

For the time being i have this code :

暂时我有这个代码:

var b = false;
$j('#tb1 td input:checkbox').each(function(){ 
      if($j(this).is(':checked')){
    b = true;
      }
});

this check if there is at least one checkbox check but if i add this to my selector :

此检查是否至少有一个复选框检查但如果我将其添加到我的选择器中:

$j('#tb1 td input:first:checkbox').each(function(){ 
      if($j(this).is(':checked')){
    b = true;
      }
});

it check for only the first row not for all the rows and only the first checkbox. How can i achieve this by using the "first" selector ? Thanks in advance

它只检查第一行而不是所有行,只检查第一个复选框。我如何通过使用“第一个”选择器来实现这一点?提前致谢

回答by Arun P Johny

Try

尝试

var b = false;
$j('#tb1 tr').find('input:checkbox:first').each(function(){ 
    if(this.checked){
        b = true;
    }
});

回答by nrsharma

Your jquerycode is wrong, you are using the correct selectorbut in wrong place..try this

你的jquery代码是错误的,你使用的是正确的selector但在错误的地方......试试这个

$j('#tb1 td input:checkbox:first').

More infor about :Firstis here

更多关于Infor的:First这里

回答by Dipesh Parmar

You are doing correct but placing of selector is wrong.

你做得对,但选择器的放置是错误的。

change

改变

$j('#tb1 td input:first:checkbox').

to

$j('#tb1 td input:checkbox:first').

回答by Rajarshi Das

Please try it

请试试

 var b = false;
  $j('#tb1 tr input:checkbox:first').each(function(){
    if(this.checked){
      b = true;
    }
  });