我希望能够在HTML中做这样的事情.它不是有效的HTML,但意图是:
<table>
<tr>
<th>Name</th>
<th>Favorite Color</th>
<th> </th>
<th> </th>
</tr>
<tr>
<form action="/updatePerson" method="post">
<input name="person_uuid" value="550e8400-e29b-41d4-a716-446655440000"/>
<td><input name="name" value="John"/></td>
<td><input name="favorite_color" value="Green"/></td>
<td><input type="submit" value="Edit Person"/></td>
</form>
<td>
<form action="deletePerson" method="post">
<input name="person_uuid" value="550e8400-e29b-41d4-a716-446655440000"/>
<input type="submit" value="Delete Person"/>
</form>
</td>
</tr>
<tr>
<form action="/updatePerson" method="post">
<input name="person_uuid" value="f47ac10b-58cc-4372-a567-0e02b2c3d479"/>
<td><input name="name" value="Sally"/></td>
<td><input name="favorite_color" value="Blue"/></td>
<td><input type="submit" value="Edit Person"/></td>
</form>
<td>
<form action="deletePerson" method="post">
<input name="person_uuid" value="f47ac10b-58cc-4372-a567-0e02b2c3d479"/>
<input type="submit" value="Delete Person"/>
</form>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
显然,我不能这样做,因为我不能在<tr>
元素内部直接使用表单标记.我能看到的唯一选择是使用讨厌的javascript或改变我的程序的行为.
什么可能是一个解决方案,允许我有一个跨越多列这样的表单?
我发现自己经常限制范围.我发现它使代码更清晰,并允许我更容易地重用变量.这在C中特别方便,其中变量必须在新范围的开头声明.
这是我的意思的一个例子.
{
int h = 0;
foreach (var item in photos)
{
buffer = t.NewRow();
h = item.IndexOf("\\x\\");
buffer["name"] = item.Substring(h, item.Length - h);
t.Rows.Add(buffer);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我限制了h的范围,而没有在每次迭代中初始化它.
但我没有看到很多其他开发人员经常这样做.这是为什么?这样做有不利之处吗?