从 html 表格单元格中获取内容值,并使用 javascript 使其显示在文本框中
Get content value from a html table cell and make it appear to the textbox using javascript
我从可滚动的HTML表格的单元格中检索值时遇到问题。我已经修改了一些javascript代码,但它们似乎不起作用。请帮助我,我对javascript很陌生。这是我的以下代码:
爪哇语
var rowIndex = 0;
var cellIndex = 0;
var nTable = "";
var nRows = 0;
var nCells = 0;
function showCoords(r,c)
{
rowIndex = r;
cellIndex = c;
if(r!=0 && nTable.rows[r].cells[c].tagName != "th")
{
var selectedValue = nTable.rows[r].cells[0].innerHTML;
document.getElementById('celldata1').value=selectedValue;
var selectedValue = nTable.rows[r].cells[1].innerHTML;
document.getElementById('celldata2').value=selectedValue;
var selectedValue = nTable.rows[r].cells[2].innerHTML;
document.getElementById('celldata3').value=selectedValue;
}
}
function getCoords(currCell)
{
for (i=0; i<nRows; i++)
{
for (n=0; n<nCells; n++)
{
if (nTable.rows[i].cells[n] == currCell)
{
showCoords(i,n);
}
}
}
}
function mapTable()
{
nTable = document.getElementsByTagName("table")[1]
nRows = nTable.rows.length;
nCells = nTable.rows[0].cells.length;
for (i=0; i<nRows; i++)
{
for (n=0; n<nCells; n++)
{
nTable.rows[i].cells[n].onclick = function()
{
getCoords(this);
}
nTable.rows[i].cells[n].onmouseover = function()
{
this.style.cursor = 'pointer';
}
}
}
}
onload=mapTable;
我不确定上面的javascript代码在语法或逻辑上是否出错,但如果有人帮助我指出它们,我会很高兴。
网页代码:-
头
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>My Content</title>
<link rel="stylesheet" text="css/text" href="mycss.css" title=""></link>
<script type ="text/javascript" src="test.js"></script>
</head>
正文部分 1
这是我希望值相应地显示在表格的每个单元格中的每个文本框中的部分。
<body>
<table border="0">
<tbody>
<tr>
<td>Content 1:</td>
<td><input id="celldata1" type="text" name="content1" value="" /></td>
</tr>
<tr>
<td>Content 2:</td>
<td><input id="celldata2" type="text" name="content2" value="" /></td>
</tr>
<tr>
<td>Content 3:</td>
<td><input id="celldata3" type="text" name="content3" value="" /></td>
</tr>
</tbody>
</table>
身体部分 2
这是表一。
<table border="1" id="tableone">
<thead>
<tr>
<th>Item</th>
<th>Content 1</th>
<th>Content 2</th>
<th>Content 3</th>
</tr>
</thead>
<tbody>
<%! int i;%>
<%for(int i=0; i<50; i++){%>
<tr>
<td><%=i+1%></td>
<td></td>
<td></td>
<td></td
</tr>
<%}%>
</tbody>
</table>
</div>
顺便说一下,如果我的英语不好,很抱歉:)
我为此使用 jQuery:
// Iterate throught rows of table:
jQuery("table").find("tr").each( function() {
var currentRow = jQuery(this);
// Iterate throught cell in current row:
currentRow.find("td").each(function() {
var currentCell = jQuery(this);
// Do what you want with cell
});
});
相关文章:
- PHP Javascript显示/隐藏按钮不工作
- 通过javascript显示和更改文本
- 是否可以用JavaScript显示等效的文件夹对话框
- 如何使用javascript显示图像
- BMI计算器以Javascript显示结果
- 单击“JavaScript显示变量”
- Javascript:显示带有复选框值的图像
- 通过ID JavaScript显示随机背景图像
- 无法在qualtrics上使用javascript显示背景图像
- 日期选择器无法使用javascript显示
- javascript 显示空白灰色层
- 页面加载后通过javascript显示图像
- 使用JavaScript显示鼠标的x和y位置
- 用javascript显示html表
- 使用 JavaScript 显示 Laravel's Variable
- javascript显示地理位置
- 搜索XML文件并使用javascript显示结果
- 使用javascript显示文件名上传
- 使用javascript显示隐藏的HTML元素
- 使用jquery和javascript显示PHP中的变量