簡體   English   中英

獲取表行內特定div的值

[英]Getting the value of a specific div inside a table row

我有一個動態表,其中包含數據庫中的數據。 每行包含4個div元素,並帶有一些文本。 我希望能夠獲得那些div之一的價值。

這是表格的一點:

function addData(Dato, BookedBy, Fra ,Til) {

              var tbl = document.getElementById('bookingListe');
              var lastRow = tbl.rows.length;

              // if there's no header row in the table, then iteration = lastRow + 1
              var iteration = lastRow;
              var row = tbl.insertRow(lastRow);

              // left cell
              var cellLeft = row.insertCell(0);
              var person = document.createElement("div");
              var dato = document.createElement("div");

              var fra = document.createElement("div");
              var til = document.createElement("div");
              var checkbox = document.createElement("input");
              checkbox.type = "checkbox";
              checkbox.name = "delete";


              dato.innerHTML ="Booket for: " + Dato;
              person.innerHTML ="Booket av: " + BookedBy;
              fra.innerHTML ="Fra: " + Fra;
              til.innerHTML ="Til: " + Til;


              cellLeft.appendChild(person);
              cellLeft.appendChild(dato);
              cellLeft.appendChild(checkbox);
              cellLeft.appendChild(til);
              cellLeft.appendChild(fra);

這是獲取這些div之一的方法。

function Slett(){
   $('#bookingListe').find('tr').each(function () {
      var row = $(this);
      if (row.find('input[type="checkbox"]').is(':checked')){
          alert(row.find('div').val());
        // Since there are 4 divs, the code above isn't going to work
      }
  });
 }

這行不通,而Javascript不是我的強項。 任何建議表示贊賞。

我建議您像div一樣添加唯一的類

var dato = document.createElement("div");
d.className = "dato";

然后,您可以使用類選擇器訪問它。 由於dato是div元素,因此您需要使用.html().text()

var dataText = row.find('.dato').html();

暫無
暫無

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

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