【发布时间】:2016-12-13 03:29:03
【问题描述】:
我有一个页面,其中有一张桌子。在表格的每一行中,都有关于在线杂货配送订单的数据。在每一行的末尾,有一个提交按钮,说明要对订单执行的操作,表单到此结束。新表单从下一行开始
我应该把
标签放在哪里?我应该将标签: html forms html-table
我有一个页面,其中有一张桌子。在表格的每一行中,都有关于在线杂货配送订单的数据。在每一行的末尾,有一个提交按钮,说明要对订单执行的操作,表单到此结束。新表单从下一行开始
我应该把
标签放在哪里?我应该将标签: html forms html-table
如果使用 javascript 是一种选择,您可以不使用<form> 标签工作吗?
我在这个例子中使用了 jQuery,数据被发布到自己($.post() 的第一个参数)。
表格
<table>
<tbody>
<tr>
<td><input name="inputfield[]" type="text" value="input1" /></td>
<td><select name="selectfield[]">
<option selected value="select1-option1">select1-option1</option>
<option value="select1-option2">select1-option2</option>
<option value="select1-option3">select1-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input2" /></td>
<td><select name="selectfield[]">
<option selected value="select2-option1">select2-option1</option>
<option value="select2-option2">select2-option2</option>
<option value="select2-option3">select2-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input3" /></td>
<td><select name="selectfield[]">
<option selected value="select3-option1">select3-option1</option>
<option value="select3-option2">select3-option2</option>
<option value="select3-option3">select3-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
</tbody>
</table>
脚本
$(".submit").on("click", function(){
var row = $(this).parents("tr");
$.post("", row.find("input, select, radio").serialize(), function(data){ console.log(data); });
});
【讨论】:
HTML5 为您的问题提供了不错的解决方案。它的名字是form 属性。只需将表单放入最后一个<td> 并从其他输入中引用其id。见例子。
<table>
<tr>
<td>
<input type="text" name="inp1" form="form1" />
</td> <!-- ^^^^ ^^^^^ -->
<td>
<form id="form1" method="get" action="/">
<!-- ^^ ^^^^^ -->
<input type="submit" name="submit1" value="submit" />
</form>
</td>
</tr>
<tr>
<td>
<input type="text" name="inp2" form="form2" />
</td>
<td>
<form id="form2" method="get" action="/"><input type="submit" name="submit2" value="submit" /></form>
</td>
</tr>
</table>
【讨论】:
form 属性。该示例仅显示了一个简短的“外部”输入。关键是将<form> 元素放在一个<td> 中。