簡體   English   中英

如何使用javascript按屬性獲取表單元格值

[英]How to get table cell value with javascript by attribute

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內的文本

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

要么

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

獲得價值嘗試一下

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

檢查小提琴

更新場

如果通過“單元格值”來查找<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' ,然后獲取父元素that的文本。

//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);
});​

更新示例

編輯

您的功能應為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM