【发布时间】:2020-03-26 18:35:28
【问题描述】:
我需要检查整个表,如果第 1 列有值(保留),并返回找到该值的那行的单元格 0 的值。
基本上点击一个按钮,运行函数并返回找到值(REVERVED)所在行的单元格0的值。
如果可以的话,我喜欢使用普通的 javascript 而不是 jquery。
如果我实际单击该行,则以下功能将起作用。
<input type="button" value="☑️ check if RESERVED exists" onclick="findValueInRow()"> //
<br>
<br>
<table id="table2">
<thead>
<tr>
<th>ID</th>
<th>Contact</th>
<th>Country</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>2</td>
<td>RESERVED</td>
<td>Mexico</td>
</tr>
<tr>
<td>1</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>3</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>4</td>
<td>RESERVED</td>
<td>Canada</td>
</tr>
<tr>
<td>5</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</body>
function findValueInRow()
{
table = document.getElementById("table2");
for(var i = 1; i < table.rows.length; i++)
{
table.rows[i].onclick = function()
{
// get the selected row index
rIndex = this.rowIndex;
var produtcID = this.cells[1].textContent;
//check to see if value RESERVED exists in the table
if(produtcID == "RESERVED" )
{
alert(this.cells[0].textContent);
}
else{
//my other code if the value is not found
}//end else
};
}
}
【问题讨论】:
-
您的实际问题是什么?你想让代码做什么?请举一个带有示例数据的表格的示例。
-
我已经用一些代码编辑了这个问题。基本上我需要单击一个按钮,运行该函数并返回找到值(REVERVED)的行的单元格 0 的值。该代码有效,只是我不想点击该行。
-
如果你喜欢一些,你可以投票或接受一个(在投票按钮下)。
标签: javascript