jQuery在表行中找到一个复选框 [英] Jquery find a checkbox inside a table row

查看:72
本文介绍了jQuery在表行中找到一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html

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

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

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.

当前,我正在这样做

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");

        }

推荐答案

您可以执行以下操作:

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

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

That uses a descendant selector for the checkbox, then gets the raw DOM element, and looks at its checked property.

选择器参考

  • 大多数浏览器支持的选择器: CSS 2.1
  • 某些浏览器和各种JavaScript库(用于与DOM交互)支持的较新选择器: CSS3选择器
  • 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

这篇关于jQuery在表行中找到一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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