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

时间:2012-10-04 20:05:44

标签: 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;
        }

2 个答案:

答案 0 :(得分:3)

试试这个

这是为了获取td

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

OR

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

获取值尝试此

$('td[abbr*="FName"][abbr*="LName"]').attr('value')

Check Fiddle

UPDATED FIDDLE

答案 1 :(得分:3)

如果按“单元格值”,您只是想在<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'

查找包含已检查输入的所有//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); });​ ,然后获取该元素父文本。

<强> 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;
}

<强> UPDATED EXAMPLE

修改

你的功能应该是:

{{1}}