[英]Get text from select in table cell
Using either raw javascript or jQuery, I need to get the text from a select that is in a table cell. 使用原始javascript或jQuery,我需要从表单元格中的选择中获取文本。 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. 这段代码的问题是它使我获得了用于选择的原始html,并且我需要知道实际选择了什么文本。 Thanks! 谢谢!
Switch to jquery! 切换到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 如果您尝试通过此功能从其中剥离html或仅使用其中的代码怎么办
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/ 对于HTML中的选定选项,请参考: 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'));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.