Form tag won't enclose elements inside a table

戏子无情 提交于 2019-12-18 19:31:43

问题


I've run into a curious problem; I've got a form inside a <tr>, however the form refuses to wrap any tags inside it. I've made a quick JSFiddle here to play around with. Firebug reports that the form isn't wrapping anything:

The <form> element is greyed out and not wrapping anything. The HTML for this test is below

<table>
    <form>
        <tr>
            <td>Input</td>
            <td>Another input</td>
        </tr>
        <tr>
            <td colspan="4"><span>Other stuff</span></td>
        </tr>
    </form>

    <tr>
        <td>
            Rows not affected by the form
        </td>
    </tr>
    <tr>
        <td>
            Rows not affected by the form
        </td>
    </tr>
</table>

As can be seen, the form holds two trs in the written markup. I read here that this is invalid, so my question is can I create a form that holds two or more trs and an arbitrary amount of other elements inside a table? The table has other rows in it not associated with the form, so putting a <form> round the entire table is unhelpful, although seeing as the other rows won't have any inputs for the form (POST request), I suppose a form could be put around the entire table.

Which is a better solution; whole-table wrap, or a working fix for just enclosing the needed rows in a form tag? I know I could put a table inside a td > form, but then the column widths wouldn't be the same in the nested table, which is why I came to ask this question.


回答1:


You cannot interrupt the <table> structure with any tags besides <thead>, <tfoot>, <tbody>, <tr>, <th>, or <td>. You form tags need to be encapsulated between two <td> or your entire <table> needs to be placed within the <form> tag.

<table>
    <tr>
        <td>
            <form>
            ...form data...
            </form>
        </td>
    </tr>
</table>

..or..

<form>
    <table>
    ...
    </table>
</form>



回答2:


you can only put a form inside a td basically, so you could put those 2 rows inside a new table that you create inside a td
like ...

<table><tr><td><form><table><tr>...</tr><tr>...</tr></table></form></td></tr><tr>...</tr><tr>...</tr></table>



回答3:


The <form> tag can only be placed inside a <td> element or outside the <table> in this case.

If I were you, I'd just put the <form> around the whole table since you said there won't be any other forms within it.

Or, you could replace the <table> completely with <div>s instead that use display: table; or display: table-cell;.



来源:https://stackoverflow.com/questions/6615903/form-tag-wont-enclose-elements-inside-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!