如果 XHTML 不允许嵌套表单,那么 how/why 这是否有效?这段代码可以使用吗?

If XHTML does not allow nested forms, then how/why does this validate? Is this code ok to use?

我正在处理一个网页,该网页列出了数据库中的多个租赁预订。我想我可能把自己逼到了墙角。我最初让每一行以 3 个单元格结尾,每个单元格包含一个由 2 或 3 个隐藏字段组成的小表格和一个 "edit"、"return" 或 "cancel" 预订按钮。

那些工作正常,并且都是有效的,因为每个完整的表单都在一个单元格中。

现在我需要添加功能以允许选择多行并将它们传递到另一个页面。所以想都没想就加了一个<form>把整个table包裹起来,每行加了一个checkbox,在table的底部加了一个提交按钮。

经过一些研究,我非常确定 XHTML 中不允许使用嵌套表单。但是文档仍然以某种方式通过 W3C 验证器进行验证。我正在使用 XHTML 1.0 Transitional。也许是因为它不是直接在另一个 <form> 中的 <form>,而是 <form> 中的 <table> 中的 <form>?允许吗?

表单按原样工作(它从顶行提交了一些额外的字段,但如果必须的话,我可以忽略它们)。

我只是不敢实施这个,因为我觉得这不可能是正确的。如果有人能澄清这是否有效,我将不胜感激。如果不是,您有任何修复建议吗?

提前致谢!

<form id="form2" name="form2" method="post" action="pickup.php">
    <table width="1200" border="1" align="center" cellpadding="5" cellspacing="0" style="border-collapse:collapse; border-color:#CCC;">
        <tr>
            <td colspan="10" bgcolor="#CCCCCC"><h2>Equipment Being Picked Up Today</h2></td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res1" id="res1" value="2278" /></td>
            <td>George Washington</td>
            <td>555-333-4444</td>
            <td><a href="product.php?sku=R6209" target="_self">Lghting kit 6209</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2278" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2278" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2278" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res2" id="res2" value="2279" /></td>
            <td>Sam Adams</td>
            <td>333-222-7777</td>
            <td><a href="product.php?sku=R8300" target="_self">camera kit 3456</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2279" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2279" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2279" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res3" id="res3" value="2280" /></td>
            <td>Bob Dole</td>
            <td>111-222-4444</td>
            <td><a href="product.php?sku=R8609" target="_self">Other item 6789</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2280" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2280" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2280" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr>
            <td colspan="10">
                <input type="submit" name="submit" id="submit" value="Pickup Selected Equipment" />
            </td>
        </tr>
    </table>
</form>

之所以有效,是因为XHTML 1.0的形式化验证是基于XML规则,而XML是对SGML的强简化修改,也就是HTML 4.01名义上是基于。因此,一些在 HTML 的所有版本中都被禁止的功能,例如嵌套形式,并没有被 XHTML 1.0 的 形式语法所禁止。基于 XML 的 DTD。 specification 表示如下:

SGML gives the writer of a DTD the ability to exclude specific elements from being contained within an element. Such prohibitions (called "exclusions") are not possible in XML.

For example, the HTML 4 Strict DTD forbids the nesting of an 'a' element within another 'a' element to any descendant depth. It is not possible to spell out such prohibitions in XML. Even though these prohibitions cannot be defined in the DTD, certain elements should not be nested. A summary of such elements and the elements that should not be nested in them is found in the normative Element Prohibitions.

并且 Element Prohibitions 表示 form 元素不得包含另一个 form 元素。

嵌套表单是不安全的。没有说明如果你这样做会发生什么。例如,没有规定在提交外部表单时是否包含内部表单上的字段。

因此,您应该考虑重构页面以避免表单嵌套。如果您需要这方面的帮助,请考虑发布一个新问题来指定所需的功能并展示您在重组方面的最佳尝试。