如何使用 JavaScript/jQuery 获取表单数据? [英] How can I get form data with JavaScript/jQuery?
本文介绍了如何使用 JavaScript/jQuery 获取表单数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的单行方式来获取表单数据,就像以经典的纯 HTML 方式提交一样?
Is there a simple, one-line way to get the data of a form as it would be if it was to be submitted in the classic HTML-only way?
例如:
<form>
<input type="radio" name="foo" value="1" checked="checked" />
<input type="radio" name="foo" value="0" />
<input name="bar" value="xxx" />
<select name="this">
<option value="hi" selected="selected">Hi</option>
<option value="ho">Ho</option>
</form>
输出:
{
"foo": "1",
"bar": "xxx",
"this": "hi"
}
这样的东西太简单了,因为它没有(正确地)包括文本区域、选择、单选按钮和复选框:
Something like this is too simple, since it does not (correctly) include textareas, selects, radio buttons and checkboxes:
$("#form input").each(function () {
data[theFieldName] = theFieldValue;
});
推荐答案
$('form').serialize() //this produces: "foo=1&bar=xxx&this=hi"
这篇关于如何使用 JavaScript/jQuery 获取表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文