n0p*_*t3x 2 html jquery serialization
我在jQuery中遇到了form.serialize()函数的问题.当我尝试通过AJAX提交序列化表单时,serialize()只返回一个空字符串.
也许我的HTML大纲有问题:
<form id="category-dynamic" class="dynamic">
<fieldset id="inner-fieldset">
<legend id="new-category">
<label for="category-name">Category Name: </label>
<input type="text" name="category-name" value="" />
</legend>
<ul id="category-fields">
<li>
<label>Field #1:</label><br />
<input type="text" name="fields[]" value="" />
</li>
<li>
<label>Field #2:</label><br />
<input type="text" name="fields[]" value="" />
</li>
</ul>
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
在我的jQuery函数中,我只需调用:
$.post("processor.php", $('#category-dynamic').serialize(), function(data){
// data handling here...
});
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题.在调试JavaScript时,我可以看到表单中的输入值,但是在调用serialize()时,结果字符串为空.
原来我在调用serialize()之前禁用了表单上的输入元素.为了解决这个问题,我在禁用之前更改了代码以检索表单值,然后在post方法中使用表单值字符串.
// Disabling the input fields breaks serialize, so get the values string first
var formValues = form.serialize();
form.find('input').attr('disabled', 'disabled');
// Now post the form to the server
$.post(this.action, formValues, function (data)
{
//...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8754 次 |
| 最近记录: |