[英]Getting the value of a specific div inside a table row
I have a dynamic table with data from a database. 我有一个动态表,其中包含数据库中的数据。 Each row contains 4 div elements, with some text. 每行包含4个div元素,并带有一些文本。 I would like to be able to get the value of one of those div's. 我希望能够获得那些div之一的价值。
Here is a little bit of the table: 这是表格的一点:
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);
Here is the method for getting one of those divs. 这是获取这些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
}
});
}
This isn't working, and Javascript isn't my strong side. 这行不通,而Javascript不是我的强项。 Any suggestions are appreciated. 任何建议表示赞赏。
I would recommend you to add unique class to div like 我建议您像div一样添加唯一的类
var dato = document.createElement("div");
d.className = "dato";
Then you can access it using class selector . 然后,您可以使用类选择器访问它。 As dato
is a div element you need to use .html() or .text() 由于dato
是div元素,因此您需要使用.html()或.text()
var dataText = row.find('.dato').html();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.