jQuery jQuery查找子复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4870494/
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-08-26 18:05:05  来源:igfitidea点击:

jQuery Find child Checkbox

jquerycheckboxhtml-table

提问by Dodinas

I have a quick jQuery question:

我有一个快速的 jQuery 问题:

I have the following type of table:

我有以下类型的表:

<table id="my_table">
  <tr>
   <td><input type="checkbox" value="24"></td>
   <td> Click Here To Check The Box </td>
   <td> Or Click Here </td>
  </tr>
 </table>

And in jQuery, I am attempting to select the corresponding checkbox in that row that was clicked.

在 jQuery 中,我试图在被点击的那一行中选择相应的复选框。

 $("#my_table tr").click(function(e) { 
     $(this).closest(":checkbox").attr("checked", checked");
  });

But the above isn't working. Any quick ideas on selecting the checkbox on the row that was clicked? Thanks!

但上面的不起作用。关于选择被点击的行上的复选框的任何快速想法?谢谢!

回答by mVChr

EDIT: BEST SOLUTION AT BOTTOM

编辑:底部的最佳解决方案

Assuming there's only one checkbox, I would attach the event to the tdinstead of the tr:

假设只有一个复选框,我会将事件附加到td而不是tr

$('#my_table td').click(function(e){
  $(this).parent().find('input:checkbox:first').attr('checked', 'checked');
});

Example

例子

If you want to be able to uncheck the checkbox as well...

如果您也希望能够取消选中复选框...

$('#my_table td').click(function(e) {
    var $tc = $(this).parent().find('input:checkbox:first'),
        tv = $tc.attr('checked');

    $tc.attr('checked', !tv);
});

This function needs to be applied to the checkbox as well to negate the default behavior.

此功能也需要应用于复选框以取消默认行为。

Example 2

示例 2

EDIT: The solution is most reasonable method is to cancel the functionality if the target is an input: http://jsfiddle.net/EXVYU/5/

编辑:如果目标是输入,解决方案是最合理的方法是取消功能:http: //jsfiddle.net/EXVYU/5/

var cbcfn = function(e) {
    if (e.target.tagName.toUpperCase() != "INPUT") {
        var $tc = $(this).parent().find('input:checkbox:first'),
            tv = $tc.attr('checked');
        $tc.attr('checked', !tv);
    }
};

$('#my_table td').live('click', cbcfn);

回答by John Hartsock

The jQuery closest()method finds the closest parent.

jQuery Nearest()方法找到最近的父级。

You really want to search child elemets, You can try the following:

你真的想搜索子元素,你可以尝试以下方法:

 $("#my_table tr").click(function(e) { 
     $(":checkbox:eq(0)", this).attr("checked", "checked"); 
  });

basically you are searching for the first checkbox found in the table row

基本上,您正在搜索表格行中的第一个复选框

some reference

一些参考

jQuery :eq() selector

jQuery :eq() 选择器

......

......

回答by Joko Wandiro

$("#my_table tr").click(function(e) {
    state= $(this).find(":checkbox:eq(0)").attr("checked");
    if (state==true){ // WITH TOGGLE
        $(this).find(":checkbox:eq(0)").removeAttr("checked");
    }else {
        $(this).find(":checkbox:eq(0)").attr("checked", "checked");
    }       
});

I make simple script to check first checkbox in specific tr. about toggle i just make it to try before post this answer.

我制作了简单的脚本来检查特定 tr 中的第一个复选框。关于切换,我只是在发布此答案之前尝试一下。

回答by Vivek

try this also...

也试试这个...

 $("#my_table tr").click(function(e) { 
 $(this).closest("type:checkbox").attr("checked", checked");
});