Which is a valid way (if any) to add a form to table rows?
I have the following situation:
<table>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
How can I add a form element and still have valid HTML?
<table>
<form>
<tr>
<td><input type="开发者_StackOverflow社区text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
Is invalid (at least I think it is)
Wrap your table inside the form element:
<form action="/" name="form1">
<table>...</table>
</form>
But even better: Build your form without tables if possible.
Tables and forms are separate concepts in HTML. People sometimes confuse them with each other. The conceptual confusion is often accompanied with confused markup. On the other hand, tables and forms can be "mixed" in a sense. Specifically, you can put a table inside a form or vice versa, and it is often useful to do so. But you need to understand what you are doing.
Tables and forms can be nested either way. But if you put forms into tables, each form must be completely included into a single table cell (one TD element in practice). Thereby the forms are each completely independent.
AFAIK It is valid to use tables in order to format the form.
<form action="/" name="form1">
<table>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
or
<table>
<tr>
<td colspan="2"> <form action="/" name="form1"></td>
</tr>
<tr>
<td><input type="text" name="q"></td>
<td><input type="text" name="a"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr><td colspan="2"> </form></td></tr>
......
</table>
精彩评论