【问题标题】:is it allowed to have a form element inside a td?是否允许在 td 中包含表单元素?
【发布时间】:2023-03-18 23:47:01
【问题描述】:

我的 html 结构如下所示:

<table class="table">
   <thead>
      <tr>
         <th>Name</th>
         <th>Email</th>
         <th>Category</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>John</td>
         <td>john@live.nl</td>
         <td>
            <form method="post" action="admin.php>
               <input type="text" ........
               <input type="text" ........
               <button type="submit" name="change_category">Update</button>
            </form>
         </td>
      </tr>
   </tbody>
</table>

当我在验证器https://validator.w3.org/ 中检查它时;它不会给我一个错误。但我不确定这是否正确......

因为,根据这个话题:Form inside a table;这是不允许的

【问题讨论】:

  • 是的,它是有效的。请仔细阅读错误,因为它们充分解释了错误以及验证器返回无效的原因...

标签: html forms html-table


【解决方案1】:

是的。表格可以放在表格的&lt;td&gt;标签内。

更新。在最后一个示例中,表单放在&lt;tr&gt; 中,这不是最佳做法,是的,但我在您的代码中看到您将表单放在&lt;td&gt; 中,完全可以。

【讨论】:

    【解决方案2】:

    是的,您可以在td 标签中添加表单,只要它包含在开始和结束标签内,就没有任何限制可以作为表单标签的父标签。

    当您有这样的表时,这是常见的用例

    <table>
      <tr>
        <td>ID></td>
        <td>Name</td>
        <td>Description</td>
        <td>Actions</td>
      </tr>
      <tr>
        <td>1</td>
        <td>First Article</td>
        <td>Description for first article</td>
        <td>
          <a href="...">View</a>
          <form method="post" action="...">
            <input type="submit" value="Delete">
          </form>
        </td>
      </tr>
    </table>

    在操作列中,我有允许删除表内特定行中的数据的操作,并且该操作是使用 POST 方法执行的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 2014-12-10
      • 2013-01-12
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2019-05-12
      相关资源
      最近更新 更多