java 如何从 HTML 表格中的选定行获取单元格值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14476487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get cell value from selected row in HTML table?
提问by uluroki
Having a table like...
有一张像...
<table border="1" style="width: 100%" id="mytab1">
<tr id="A1">
<td>100</td>
<td>John</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
<tr id="A2">
<td>101</td>
<td>Jane</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
I'm trying to get the value of the first cell in a row, in which I've pressed the "Submit" button. I'm a newbie in this. This is an example, but I'm really working on a dynamically created html table in a jsp page, similar to this How to get cell data of specific row from the dynamically created HTML table?question. I just can't get it working.
我正在尝试获取连续第一个单元格的值,其中我按下了“提交”按钮。我是这方面的新手。这是一个示例,但我确实在 jsp 页面中处理动态创建的 html 表,类似于如何从动态创建的 HTML 表中获取特定行的单元格数据?问题。我只是无法让它工作。
回答by Rajesh
Use hidden field in each row and check for the submitted value..as below:
在每一行中使用隐藏字段并检查提交的值..如下:
<table border="1" style="width: 100%" id="mytab1">
<tr id="A1">
<td>100</td>
<td>John</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="hidden" name="rowId" value="A1" />
<input type="submit" value="Submit" />
</td>
</tr>
<tr id="A2">
<td>101</td>
<td>Jane</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="hidden" name="rowId" value="A2" />
<input type="submit" value="Submit" />
</td>
Use the same name for all the hidden fields, so that in the controller you can fetch the submitted value
对所有隐藏字段使用相同的名称,以便在控制器中您可以获取提交的值
回答by Aleksei Egorov
This type of work javascript does very well.
这种类型的工作 javascript 做得很好。
- Change submit to buttons with handler;
- Put the handler into head
- 使用处理程序更改提交到按钮;
- 将处理程序放入头部
The resulted code:
结果代码:
<html>
<head>
<script>
function submitter(btn) {
var param = btn.parentElement.parentElement.id;
var myForm = document.forms["myForm"];
myForm.elements["param"].value = param;
myForm.submit();
}
</script>
</head>
<body>
<form action="#someurl" id="myForm">
<input type="hidden" name="param" />
<table border="1" style="width: 100%" id="mytab1">
<tr id="A1">
<td>100</td>
<td>John</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="button" value="Submit" onclick="submitter(this)" />
</td>
</tr>
<tr id="A2">
<td>101</td>
<td>Jane</td>
<td>Doe</td>
<td>Someplace</td>
<td>
<input type="button" value="Submit" onclick="submitter(this)" />
</td>
</tr>
</table>
</form>
</body>
</html>