Jquery 在表格行中找到一个复选框

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

Jquery find a checkbox inside a table row

jquery

提问by sarsnake

My html

我的html

<TR class="datarow" id="rowId"><TD>1</TD><TD>895171</TD><td class="classID"><INPUT type="checkbox" /></TD></TR>

how do I use Jquery to find out whether the checkbox in this particular row is checked or not. Assume that i know the unique rowId.

我如何使用 Jquery 找出此特定行中的复选框是否已选中。假设我知道唯一的 rowId。

Currently, I am doing this

目前,我正在这样做

var checkbox = $('#' + rowId + " td input:checkbox");

        if (checkbox.checked) {
           alert("checked");
        } else {
           alert("unchecked");
        }

But this doesn't seem to detect when the checkbox is checked.

但这似乎没有检测到复选框何时被选中。

EDITED Curiously, the following didn't work either:

奇怪的是,以下内容也不起作用:

        var curRow = $('#' + curRowId);
        var checkbox = $(curRow).find('input:checkbox').eq(0);



        if (checkbox.checked) {
           alert("checked");

        } else {
           alert("unchecked");

        }

回答by T.J. Crowder

You can do this:

你可以这样做:

var isChecked = $("#rowId input:checkbox")[0].checked;

That uses a descendant selectorfor the checkbox, then gets the raw DOM element, and looks at its checkedproperty.

它为复选框使用后代选择器,然后获取原始 DOM 元素,并查看其checked属性。

References for selectors:

选择器参考:

  • Selectors supported by most browsers: CSS 2.1
  • Newer selectors supported by some browsers and by various JavaScript libraries (for interacting with the DOM): CSS3 Selectors
  • 大多数浏览器支持的选择器:CSS 2.1
  • 一些浏览器和各种 JavaScript 库(用于与 DOM 交互)支持的较新选择器:CSS3 选择器

回答by Andrew Whitaker

Updated:

更新:

var rowId = "";
var checked = $("#" + rowId + " input:checkbox")[0].checked;

Your original code does not work because you're calling checkedon a jQuery result set. You need to get out the actual DOM element, which you can do by calling get()(Or by indexing the object directly like in T.J.'s answer). Since there's only one result, you want the element at index 0 in the result set.

您的原始代码不起作用,因为您正在调用checkedjQuery 结果集。您需要获取实际的 DOM 元素,您可以通过调用get()(或者像 TJ 的回答那样直接索引对象)来完成。由于只有一个结果,您需要结果集中索引 0 处的元素。

Editafter seeing updated code in OP:

在 OP 中看到更新的代码后进行编辑

Using eq()reduces the matched elements to the one at the specified index. Effectively this will return a jQuery result set with one matched object--the one you requested at the specified index. It stilldoesn't return a DOM element, which you need to use the checkedproperty.

Using将匹配的元素减少到指定索引处的元素。实际上,这将返回一个 jQuery 结果集,其中包含一个匹配的对象——您在指定索引处请求的对象。它仍然不返回 DOM 元素,您需要使用该属性。eq()checked

回答by Ilia Draznin

If you only have one checkbox per row this is the quickest way

如果每行只有一个复选框,这是最快的方法

!!$('#rowId input:checked').length

You can also omit the !! and check against '>0' but I think the !! is actually faster

你也可以省略!! 并检查 '>0' 但我认为 !! 实际上更快

回答by amit_g

This would do it

这会做到

var checked = $("#rowId" + rowId + " td.classID input:checkbox").is(":checked");