我有一个由成对文本字段组成的表单。用户可以根据需要创建任意数量的对并填写字段。字段行将添加到表的底部。 以下是格式示例:
<form id="dataForm">
<table id="textTable">
<tbody>
<tr>
<td><h2>Dancer</h2></td>
<td><h2>Place</h2></td>
</tr>
<tr>
<td><input type="text" name="Dancer0" size="15" id="Dancer0"></td>
<td><input type="text" name="Place0" size="15" id="Place0"></td>
</tr>
<tr>
<td><input type="text" name="Dancer1" size="15" id="Dancer1"></td>
<td><input type="text" name="Place1" size="15" id="Place1"></td>
</tr>
</tbody>
</table>
<input type="button" id="newRow" value="New Row" accesskey="/" onclick="moreFields();">
<input type="button" value="Update" name="Submit" onclick="submitForm(this.form);">
</form>
我遇到的麻烦是使用 ajax 将表单正确提交到外部 php 页面。这是我现在正在使用的功能:
function submitForm(form)
{
jQuery.ajax({
type: "POST",
url: <?php echo '"'.dirname(curPageURL()).'/ResultsUpdater.php"' ?>,
data: form.serialize(),
success: function(msg){
alert(msg);
}
});
return false;
}
在其他示例中,我看到他们使用 $ 来表示 jQuery,并将其放在“form”前面:$(form).serialize()
和$.ajax({...
但我的服务器不喜欢 $
。我也尝试过jQuery.form.serialize()
,但这也不起作用。我在控制台中收到的错误始终是 "TypeError: 'undefined' is not an object (evaluating 'jQuery.form.serialize')"
如果我可以向 php 文件提交一堆键值对(据我所知,serialize() 是这样做的),那就太好了。
您可以尝试使用表格 here
只需选择下拉项目之一即可查看表单。
谢谢
最佳答案
你可以这样做:
jQuery("#dataForm").serialize();
我已在您引用的页面上成功尝试过。
由于 $
在您的页面上未定义,我最好的猜测是您加载了一些将 $
重置为未定义的脚本。最好的方法是改用 jQuery
,因为 $
只是 jQuery
的别名。
关于javascript - 使用ajax提交包含未知数量字段的html表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441559/