我有2张桌子: 1.)升级表(FK- source_id 2.)源表(PK-source_id) 我先打印升级表。 例如:升级表输出: SourceID状态通知 1开放名称1 2关闭name2 3开名3
单击升级表的row1中的任意位置,我需要获取source_id并打印源表的数据。因此与row2,row3等同样的东西..
我目前的代码包含在这篇文章中:这里我正在打印升级表,但是在点击行我再次打印相同的数据。如何在点击行打印源表的数据?请帮忙。
(function () {
if (window.addEventListener) {
window.addEventListener('load', run, false);
} else if (window.attachEvent) {
window.attachEvent('onload', run);
}
function run() {
var t = document.getElementById('myTable');
t.onclick = function (event) {
event = event || window.event; //IE8
var target = event.target || event.srcElement;
while (target && target.nodeName != 'TR') { // find TR
target = target.parentElement;
}
//if (!target) { return; } //tr should be always found
var cells = target.cells; //cell collection - https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement
//var cells = target.getElementsByTagName('td'); //alternative
if (!cells.length || target.parentNode.nodeName == 'THEAD') {
return;
}
var f1 = document.getElementById('sourceID');
var f2 = document.getElementById('status');
var f3 = document.getElementById('notice');
f1.value = cells[0].innerHTML;
f2.value = cells[1].innerHTML;
f3.value = cells[2].innerHTML;
//console.log(target.nodeName, event);
};
}
})();
<table border="1" id="myTable" cellspacing="1">
<tr>
<th>sourceID</th>
<th>Status</th>
<th>Notice</th>
</tr>
<tr>
<tr>
<td>1</td>
<td>open</td>
<td>name1</td>
</tr>
<tr>
<td>2</td>
<td>close</td>
<td>name2</td>
</tr>
<tr>
<td>3</td>
<td>open</td>
<td>name3</td>
</tr>
</table>
<table border="1">
<tr>
<th>sourceID</th>
<th>Name</th>
<th>Notice</th>
</tr>
<tr>
<td>sourceID is:
<input type="text" id="sourceID" />
</td>
<td>status is:
<input type="text" id="status" />
</td>
<td>notice is:
<input type="text" id="notice" />
</td>
</tr>
</table>
</body>