我正在尝试表单序列化,但令人惊讶的是它没有序列化表单。这是代码:
<div id="content">
</div>
<form id= "myform">
<input type="text" id="inp"value="mytext">
<input type="button" id="btn" value="serialize"/>
</form>
这是我正在使用的 jQuery 代码:
$("form").submit(function(e){
e.preventDefault();
var v= $(this).serialize();
console.log(v);
});
这是fiddle
最佳答案
您的输入
字段需要一个name
属性。否则它们会被 jQuery 的 .serialize()
忽略。
这是一个报价from the docs :
Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.
这是您对名称属性的 fiddle : http://jsfiddle.net/6fgUg/28/
关于jQuery 序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9074627/