Php jquery重新填充表表单字段
我有一个简单的html表,它从两行开始。每行包含2个表单字段。用户可以按如下方式向表中添加其他行:Php jquery重新填充表表单字段,php,javascript,jquery,html,forms,Php,Javascript,Jquery,Html,Forms,我有一个简单的html表,它从两行开始。每行包含2个表单字段。用户可以按如下方式向表中添加其他行: $('a#addRow').click(function() { $('#jTable tr:last').after('<tr>'+ '<td><input type="text" name="foo[]" value="" id="foo[]" /></td>'+ '<td><input
$('a#addRow').click(function() {
$('#jTable tr:last').after('<tr>'+
'<td><input type="text" name="foo[]" value="" id="foo[]" /></td>'+
'<td><input type="text" name="bar[]" value="" id="bar[]" /></td></tr>');
return false;
});
这个很好用
但是,如果在发布表单后检测到错误,则需要使用正确的行数重新创建表。目前,在发布之后,表默认只返回到两个起始行。尝试将数据从标准html发布转移到AJAX请求。这将确保在发布表单时不会重新加载站点->您的表将保持原样。请参见提交表单上的,您可以在表中存储行数
$('form').submit(function(){
// Cool stuff here
// ...
localStorage.setItem('nbLinesinTable', $('table > tr').length);
// or add input to form
}
k_wave的解决方案也是可靠的。我决定在服务器端这样做 控制器:
$data['rows'] = count($_POST('foo'));
看法
<?php for ($i = 0; $i < $rows; $i++): ?>
<tr>
<td><input type="text" name="foo[]" value=<?php echo $foo[$i]; ?> id="foo[]" /></td>
<td><input type="text" name="bar[]" value=<?php echo $bar[$i]; ?> id="bar[]" /></td>
</tr>
<?php endfor; ?>