jQuery 序列化不起作用

标签 jquery

我正在尝试表单序列化,但令人惊讶的是它没有序列化表单。这是代码:

<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/

相关文章:

javascript - 使用提交按钮提交表单时如何将javascript变量发送到php中的另一个页面

jQuery UI 选项卡插件损坏

javascript - 如何从数据库获取时间后开始倒计时?

javascript - Rails 应用程序 : require bootstrap vs require bootstrap-sprockets

jquery - JavaFX 应用程序中的函数未定义错误

javascript - chrome 下可以按数字键,firefox 下不行?

javascript - 如果我在 mvc 中通过 jquery 填充字段,则显示必需的验证错误

javascript - 至少一组所有html元素填充jquery

jquery - 如何插入带有图标的 jQuery-Mobile 按钮?

jquery - 更改 div 的内容 - jQuery