关于原生document.getElementById("a")与$("#a")[0]

document.getElementById("a")表示dom元素,

$("#a")[0]表示jquery元素,

document.getElementById("a")可以等价于$("#a")[0]

``` <% List<test_information> list = (List<test_information&gt;)session.getAttribute(&quot;date&quot;); String jsonString = new com.google.gson.Gson().toJson(list); %> <div id=&quot;overlay&quot; style=&quot;display:none; position:fixed; top:0; left:0; width:100%; height:100%; background-color:rgba(0,0,0,0.5); z-index:99;&quot;></div> <div class = &quot;tit&quot;>查看凭证</div> <div class = &quot;Ribbon&quot;> <table class = &quot;R_t&quot;> <tr> <td style=&quot;white-space: nowrap; border-right: none !important;&quot; class = &quot;left_Ribbon&quot;> <!-- 按钮1 --> <input type=&quot;button&quot; value=&quot;传统记账&quot; class=&quot;R_button_1&quot; onclick = &quot;accounting.show()&quot;> <!-- 表单2 - 导出 --> <form action=&quot;&quot; class=&quot;inline-form&quot;> <input type=&quot;submit&quot; value=&quot;导出&quot; class=&quot;R_button&quot;> </form> <!-- 表单3 - 打印 --> <form id=&quot;printForm&quot; action=&quot;white.jsp&quot; method=&quot;post&quot; class=&quot;inline-form&quot;> <input type=&quot;hidden&quot; id=&quot;selectedTelsHiddenField&quot; name=&quot;telsToPrint&quot;> <input type=&quot;submit&quot; value=&quot;打印&quot; onclick=&quot;return submitSelectedTels();&quot; class=&quot;R_button&quot;> </form> <!-- 按钮4 --> <input type=&quot;button&quot; value=&quot;模板&quot; onclick=&quot;template.show()&quot; class=&quot;R_button&quot;> </td> <td class = &quot;search_box&quot;> <input type = &quot;text&quot; name = &quot;search_name&quot;class = &quot;search_text&quot; placeholder = &quot;搜索关键字&quot;> <input type=&quot;button&quot; value=&quot;搜索&quot; onclick=&quot;performSearch()&quot; class = &quot;search_button&quot;> </td> </tr> </table> </div> <div class=&quot;body&quot;> <table class=&quot;cer&quot; > <% for(test_information news:list) { %> <!-- 将每条记录分为两列展示 --> <tr class = &quot;head_tr&quot;> <td class=&quot;zero_box&quot;> <input type=&quot;checkbox&quot; name=&quot;selectedTels&quot; value=&quot;<%=news.getTel()%>&quot; onchange=&quot;updateSelection(this)&quot; style = &quot;zoom : 300%&quot;> </td> <td class=&quot;first_box&quot;> <table> <tr> <td class=&quot;material_box&quot;><%= news.getMaterial() %></td> </tr> <tr> <td class=&quot;time_box&quot;><%= news.getStart_time() %></td> </tr> </table> </td> <td class=&quot;second_box&quot;> <%= news.getWashing_method() %> <br/> <%= news.getPrice() %> </td> <td class=&quot;third_box&quot;> <form action =&quot;&quot;> <input name = &quot;id&quot; value =&quot;<%=news.getTel() %>&quot; type = &quot;hidden&quot;> <input type =&quot;submit&quot; class = &quot;func_button&quot; value = &quot;税费&quot; ><br> <input type =&quot;submit&quot; class = &quot;func_button&quot; value = &quot;查看凭证&quot; ><br> <input type =&quot;submit&quot; class = &quot;func_button&quot; value = &quot;删除凭证&quot; > </form> </td> </tr> <% } %> </table>```怎么解决吧list转换成json格式后再table中的for循环无法使用的问题
03-20
从您的代码来看,您已经将 `list` 转换成了 JSON 格式 (`jsonString`),但是随后在 JSP 的 `<% for(test_information news:list) { %>` 循环中仍然尝试直接使用原始的 `list` 对象。这是因为一旦将列表序列化为 JSON 字符串后,原始的 Java 列表对象就不再可用。 ### 解决方案 如果您希望既生成 JSON 数据又能在页面上通过 JSP 循环渲染数据,则需要分别处理这两种需求: #### 方案一:保留原生 Java 列表用于 JSP 渲染 如果还需要在 JSP 页面中遍历 `list`,那么不要将其转换为 JSON 格式,而是保持其作为 Java 对象的状态: ```jsp <% // 直接使用 session 中的 list 进行渲染 List<test_information> list = (List<test_information&gt;) session.getAttribute(&quot;date&quot;); if (list != null && !list.isEmpty()) { for (test_information news : list) { %> <tr class=&quot;head_tr&quot;> <td class=&quot;zero_box&quot;> <input type=&quot;checkbox&quot; name=&quot;selectedTels&quot; value=&quot;<%=news.getTel()%>&quot; onchange=&quot;updateSelection(this)&quot; style=&quot;zoom: 300%;&quot;> </td> <td class=&quot;first_box&quot;> <table> <tr><td class=&quot;material_box&quot;><%= news.getMaterial() %></td></tr> <tr><td class=&quot;time_box&quot;><%= news.getStart_time() %></td></tr> </table> </td> <td class=&quot;second_box&quot;> <%= news.getWashing_method() %> <br/> <%= news.getPrice() %> </td> <td class=&quot;third_box&quot;> <form action=&quot;&quot;> <input name=&quot;id&quot; value=&quot;<%=news.getTel()%>&quot; type=&quot;hidden&quot;> <input type=&quot;submit&quot; class=&quot;func_button&quot; value=&quot;税费&quot;><br> <input type=&quot;submit&quot; class=&quot;func_button&quot; value=&quot;查看凭证&quot;><br> <input type=&quot;submit&quot; class=&quot;func_button&quot; value=&quot;删除凭证&quot;> </form> </td> </tr> <% } } else { out.println(&quot;<tr><td colspan=&#39;4&#39;>无数据</td></tr>&quot;); } %> ``` 在此种情况下,您可以继续访问原始的 `list` 并正常地进行循环操作。 --- #### 方案二:仅依赖 JSON 数据并移除服务器端逻辑 如果您更倾向于前端完全接管表格的构建任务(比如利用 JavaScript 来解析显示数据),可以改用以下方式: **步骤1:** 将 JSON 数据传递到客户端。 ```jsp <script> var jsonData = &#39;<%= new com.google.gson.Gson().toJson(session.getAttribute(&quot;date&quot;)) %>&#39;; var dataList = JSON.parse(jsonData.replace(/&quot;/g, &#39;&quot;&#39;)); // 替换单引号等特殊字符 </script> ``` **步骤2:** 使用 JavaScript 动态生成 HTML 表格内容。 ```html <div class=&quot;body&quot;> <table id=&quot;dynamicTable&quot; class=&quot;cer&quot;></table> </div> <script> function renderTable(data) { const tableBody = document.getElementById(&#39;dynamicTable&#39;); data.forEach(function(item) { const row = document.createElement(&#39;tr&#39;); row.className = &#39;head_tr&#39;; // 第一列:复选框 const checkboxCell = document.createElement(&#39;td&#39;); checkboxCell.className = &#39;zero_box&#39;; const checkBoxInput = document.createElement(&#39;input&#39;); checkBoxInput.type = &#39;checkbox&#39;; checkBoxInput.name = &#39;selectedTels&#39;; checkBoxInput.value = item.tel; checkBoxInput.onchange = function () { updateSelection(this); }; checkBoxInput.style.zoom = &#39;300%&#39;; // 设置样式 checkboxCell.appendChild(checkBoxInput); // 添加其他单元格... // 示例第二列的内容 const firstColumnDiv = document.createElement(&#39;td&#39;); firstColumnDiv.className = &#39;first_box&#39;; const materialInfoRow = document.createElement(&#39;tr&#39;); const timeInfoRow = document.createElement(&#39;tr&#39;); const materialTd = document.createElement(&#39;td&#39;); materialTd.className = &#39;material_box&#39;; materialTd.textContent = item.material || &#39;&#39;; const timeTd = document.createElement(&#39;td&#39;); timeTd.className = &#39;time_box&#39;; timeTd.textContent = item.start_time || &#39;&#39;; materialInfoRow.appendChild(materialTd); timeInfoRow.appendChild(timeTd); const innerTable = document.createElement(&#39;table&#39;); innerTable.appendChild(materialInfoRow); innerTable.appendChild(timeInfoRow); firstColumnDiv.appendChild(innerTable); row.appendChild(checkboxCell); row.appendChild(firstColumnDiv); // 更多动态添加... tableBody.appendChild(row); }); } window.onload = function () { renderTable(dataList); // 初始化加载数据 }; function updateSelection(element) { console.log(`Checkbox with value ${element.value} is now ${(element.checked ? &#39;checked&#39; : &#39;unchecked&#39;)}`); } </script> ``` 此方法避免了服务端复杂的数据绑定逻辑,并交由浏览器负责 DOM 构建。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值