Using either raw javascript or jQuery, I need to get the text from a select that is in a table cell. I have attached the code that I have gotten to work and almost get me what I need.
var tbl = document.getElementById('tblHours');
var rowCount = tbl.rows.length;
var colCount = tbl.rows[0].cells.length;
for (var i = 0;i<rowCount;i++) {
var myrow = tbl.rows[i];
for (var j=0;j<colCount;j++) {
alert(myrow.cells[j].firstChild.innerHTML);
}
}
The problem with this code is it gets me the raw html for the select, and I need to know what text is actually selected. Thanks!
Switch to jquery!
var tbl = $('#tblHours');
tbl.find('tr').each(function(){
$(this).find('td').each(function(){
alert($(this).find('select :selected').val());
});
});
I didnt test code. Just for example.
What if you try stripping html from it by this function or just use the code from it
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
for (var i = 0;i<$('#tblHours tr').length;i++) {
var myrow = $('#tblHours').find('td');
for (var j=0;j<myrow .length;j++) {
alert(myrow.text());
}
}
For the selected option in HTML select refer to : http://api.jquery.com/selected-selector/
Try this code (not tested):
var tbl = document.getElementById('tblHours');
var rowCount = tbl.rows.length;
var colCount = tbl.rows[0].cells.length;
for (var i = 0;i<rowCount;i++) {
var myrow = tbl.rows[i];
for (var j=0;j<colCount;j++) {
alert($(myrow.cells[j]).find('select :selected'));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.