javascript - 如何在javascript中将选定的表行值发送到另一个页面
问题描述
我有 2 页和 2 个表,在第 1 页(表 1)我想将选定的行发送到第 2 页(表 2),在表 2 中我显示选定的行
这是第 1 页中的第一个表:
<table class="src-table">
<tr>
<th>Select</th>
<th>Firstname</th>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>Jill</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>Eve</td>
</tr>
</table>
<br>
<input type="button" value="Submit" id="submit">
这是第 2 页中的第二个表格:
<table class="target-table">
<tr>
<th>Select</th>
<th>Firstname</th>
</tr>
</table>
解决方案
如果你真的需要这个。您可以使用localStorage
.
localStorage
不在沙箱中工作。但是您也可以在您的应用程序中使用它。
storeItems
当您需要保存所选内容以存储(例如在元素选择上)时运行在目标表页面上的appendStoredToAnouther
窗口事件上运行window.onload
function storeItems() {
const selectedItems = document.querySelectorAll("#first-table .selected");
const selectedHtml = nodeListToString(selectedItems);
localStorage.add('selectedTableItems', selectedHtml);
}
function nodeListToString(nodeList) {
let string = '';
nodeList.forEach(function(node) {
string += node.outerHTML;
});
return string;
}
function appendStoredToAnouther() {
const itemsHtml = window.localStorage.get('selectedTableItems');
const targetTable = document.getElementById('target-table');
targetTable.innerHTML = itemsHtml + targetTable.innerHTML;
}
<table id="first-table">
<tr class="selected">
<td>1</td>
<td>Selected</td>
<td>Item</td>
</tr>
<tr class="selected">
<td>1</td>
<td>Selected</td>
<td>Item</td>
</tr>
<tr>
<td>2</td>
<td>Not Selected</td>
<td>Item</td>
</tr>
</table>
<button type="button" onclick="storeItems()">Send to anouther</button>
<button type="button" onclick="appendStoredToAnouther()">Append stored to anouther</button>
<table id="target-table">
<tr class="selected">
<td>1</td>
<td>Selected</td>
<td>Item</td>
</tr>
<tr>
<td>2</td>
<td>Not Selected</td>
<td>Item</td>
</tr>
</table>
推荐阅读
- xamarin - 检测 Xamarin Forms 中的网络更改
- java - 在“::”双冒号表示法中内省方法引用?
- javascript - 选中复选框上的 Javascript 克隆此 div,未选中删除此 div
- java - 对对象的 ArrayList 进行排序
- sql - SQL Server:根据多个匹配值提取多个字符串
- listview - 如何在 Xamarin C# 中绑定数据列表视图
- java - Android Studio - 无法识别语法错误
- hibernate - 如何在 Spring Boot JPA 中加入两个表,我的代码出错了
- spring - com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2009 using Sprint Boot with JMS (IBM MQ)
- android - 如何使表格宽度足够大以适合其内容