如何从 jquery 生成的输入中检索数据以使用 php $_POST 处理它们?
(function($){
$countForms = 1;
$.fn.addForms = function(idform){
var myform = "<table>"+
" <tr>"+
" <td>Field A ("+$countForms+"):</td>"+
" <td><input type='text' name='field["+$countForms+"][a]'></td>"+
" <td>Field B ("+$countForms+"):</td>"+
" <td><textarea name='field["+$countForms+"][b]'></textarea></td>"+
" <td><button>remove</button></td>"+
" </tr>"+
"</table>";
if(idform=='mybutton'){
myform = $("<div>"+myform+"</div>");
$("button", $(myform)).click(function(){ $(this).parent().parent().remove(); });
$(this).append(myform);
$countForms++;
}
};
})(jQuery);
$(function(){
$("#mybutton").bind("click", function(e){
e.preventDefault();
var idform=this.id;
if($countForms<3){
$("#container").addForms(idform);
}
});
});
html/php 代码:
if ( isset( $_POST['field'] ) )
{
echo '<table>';
foreach ( $_POST['field'] as $diam )
{
// here you have access to $diam['top'] and $diam['bottom']
echo '<tr>';
echo ' <td>', $diam['a'], '</td>';
echo ' <td>', $diam['b'], '</td>';
echo '</tr>';
}
echo '</table>';
}
?>
<body>
<div>
<form method="post" name="b" >
<table>
<tr>
<td>Field A</td>
<td><input type='text' name='field[0][a]'></td>
<td>Field B</td>
<td><textarea name="field[0][b]"></textarea></td>
<td><button>remove</button></td>
</tr>
</table>
</div>
<div id="container"></div>
<button id="mybutton">add form</button>
<div align="center">
<p><input type="submit" value="Registar" name="registar"></p>
</div>
</form>
我无法从 jquery 在加载页面后生成的重复输入中检索数据。我知道,因为 foreach 只是回显 html 部分中存在的输入值。有什么建议我可以获取 jquery 输入的值以用 php 处理它们吗?