javascript - 如何使用javascript按属性获取表格单元格值

标签 javascript jquery html flexigrid

function getSelectedCopyDates() {
            var arr = new Array();
                //for every row that has a checked checkbox
                $("tr").has(".noteCheckBox:checked").each(function (i) {
                    if ($(this).id !== "checkAllNotes"){//since this doesn't have a "abbr=..." it breaks the code below "# syntax error"
                        //push the value of column(FName, LName) into the array 
                        arr.push($("#" + this.id + "> td[abbr='EventDate'] > div").text());
                    }
                });
            return arr;
        }

最佳答案

如果通过“单元格值”您只是想获取 <td> 内的文本那么你可以做类似 this 的事情:

HTML:

<table>
    <tr>
        <td align="center" abbr="FName, LName">RAWR</td>
    </tr>        
</table>​

jQuery:

$("td[abbr='FName, LName']").text();

您可以使用 jQuery 的 .text() 方法获取给定元素内的值。

编辑:

看到您只需要获取 <td>它们包含已选中的复选框,因此这可能适合您:

$("td[abbr='FName, LName'] > input:checked").parent().text(); 

查找所有td[abbr='FName, LName'包含已检查的输入,然后获取该元素父元素的文本。

<强> EXAMPLE

//You won't need the on change event for you code. I only added it here to show you what happens when there are values and when there are no values.

$("input").on("change", function(){
    var arr = new Array();

    //for every row that has a checked checkbox
    $("tr").has(".noteCheckBox:checked").each(function(i){
       //push the value of column 5 (FName, LName) into the array 
       arr.push($("#"+this.id + "> td > div.c5").text());       
    });
    //Print the array to the console.
    console.log(arr);
});​

<强> UPDATED EXAMPLE

编辑:

你的函数应该是:

function getSelectedInvestigatorNames() {
   var arr = new Array();

   //for every row that has a checked checkbox
   $("tr").has(".noteCheckBox:checked").each(function(i){
      //push the value of column 5 (FName, LName) into the array 
      arr.push($("#"+this.id + "> td[abbr='FName, LName'] > div").text());       
   });

   return arr;
}

关于javascript - 如何使用javascript按属性获取表格单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735018/

相关文章:

javascript - 使 img 标签完全填充页面而不创建滚动条

javascript - GMap - 如果光标不在标记顶部,则 onrightclick 事件不会触发

jquery - 如何重现这种滑动效果?

html - Eric Meyers Reset 上的 CSS 覆盖

html - 缩小时文本变长

javascript - 在移动页面上展开/折叠

JavaScript 返回未定义

javascript - 如果 AngularJS 复选框被过滤掉然后返回,则它们不会保持选中状态

javascript - Kendo UI 网格绑定(bind)错误

javascript - 如何使用 JQuery 基于保存在本地存储中的内容向元素添加类?