javascript - 使用循环将 id 添加到复选框

标签 javascript html node.js web ejs

我正在使用 Node js 和 Express 将 JSON 显示为 HTML 表。 现在表格显示我想添加一个复选框列来跟踪用户选择的行。 所以我在循环中添加了一个复选框,用于从 JSON 生成表。 但我无法使用循环变量向每个复选框添加 id。

如果有人可以帮助我解决这个问题,我会非常非常高兴。

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="/css/table.css">
 </head>
<body align="left">
 <form action="/results" method="post">
<div class="container">
<table class="table" align="left">
      <thead>
        <tr>
        <th>Download </th>
        <th>Title</th>
        <th>time </th>
        <th>seeds </th>
        <th> size </th>
        <th> provider </th>
        <th> link </th>
      </thead>


  <tbody>
      <% for (var i = 0; i < torrents.length; i++) { %>
        <tr>
          <td> <input type="checkbox" id=<%i%> > </td> <!-- the problematic line-->
          <td> <%= torrents[i].title%> </td>
          <td> <%= torrents[i].time%> </td>
          <td> <%= torrents[i].seeds%> </td>
          <td> <%= torrents[i].size%> </td>
          <td> <%= torrents[i].provider%> </td>
          <td> <%= torrents[i].link%> </td>
   </tr>
<% }%>
  </tbody>
</table>
</body>
</div>


<input type="submit" class="ghost-button" value="Download selected torrents to seedbox">
</form>
</html>

最佳答案

您使用了<% 'Scriptlet'标签,这是用于控制流,无输出

应该是<%=

关于javascript - 使用循环将 id 添加到复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459729/

相关文章:

javascript - HTML5 视频在 IE9 RC 中失败,但在 IE9 Beta 中失败

javascript - JSF2 Primefaces 输入键 keycode=13 不适用于 commandButton

java - dhtmlxform.send 响应在警报提示中正确显示,但字符串比较不起作用

javascript - .click() 函数与 ajax 一起使用

php - 为什么 Chrome 中的 HTML 列表中有如此多的垂直偏移 - 来自 PHP 或样式的空白?

mysql - Sails + MySQL 重新连接并尝试

javascript - Gulp concat 按顺序

javascript - Node : How to do nightmareJS e2e-testing on running docker application

javascript - 如何将函数参数用作变量的一部分?

javascript - 在 React.js 中将 props 传递给父组件