I want to get the value of all elements in a form on submit button. I serialize the data in the form but for some reasons that I don't understand in the serialised data the value of selected option on select field is not included there.
我想在提交按钮上获取表单中所有元素的值。我在表单中序列化数据但由于某些原因我在序列化数据中不理解,选择字段中所选选项的值不包括在那里。
Here's a Js fiddle
这是一个Js小提琴
My form looks like this:
我的表单看起来像这样:
<form id='foo'>
<p>
<label>Message</label>
<textarea id='message' name='message' rows='5'></textarea>
</p>
<br/>
<p>
<label>Name</label>
<select id = "name">
<option value = "1">one</option>
<option value = "2">two</option>
<option value = "3">three</option>
<option value = "4">four</option>
</select>
</p><br/>
<div id='success'></div>
<button type='submit'>Send</button>
</form>
<p id="showdata">
</p>
On submit of this form I some jquery code that handles it. It looks like this :
在提交此表单时,我会处理一些jquery代码。它看起来像这样:
// Bind to the submit event of our form
$("#foo").submit(function(event){
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// Serialize the data in the form
var serializedData = $form.serialize();
$('#showdata').html(serializedData);
});
Can somebody help me to understand where is my problem! Thank you in advance.
有人可以帮我理解我的问题在哪里!先感谢您。
3 个解决方案
#1
1
Forms work on the name, and not ID attribute. Give you select element a name value and it will work.
表单适用于名称,而不是ID属性。为您选择元素一个名称值,它将起作用。
#2
1
Your code seems to correct but you have a mistake. Look at select
, it has no attribute name
. So, $form.serialize()
will not work on this element.
你的代码似乎是正确的,但你有一个错误。看看select,它没有属性名称。因此,$ form.serialize()将不适用于此元素。
This is fixed by add attribute name to select
通过添加属性名称来修复此问题
<form id='foo'>
<p>
<label>Message</label>
<textarea id='message' name='message' rows='5'></textarea>
</p>
<br/>
<p>
<label>Name</label>
<select name="test">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
</p><br/>
<div id='success'></div>
<button type='submit'>Send</button>
</form>
<p id="showdata">
</p>
More information : Form Serialize
更多信息:表单序列化
#3
0
If you use $form.serialize()
in select element you should add attribute name in there because $form.serialize()
will get values from attribute name
如果在select元素中使用$ form.serialize(),则应在其中添加属性名称,因为$ form.serialize()将从属性名称中获取值
Update
https://jsfiddle.net/drhe1L9u/
Add attribute name to select element
添加属性名称以选择元素
#1
1
Forms work on the name, and not ID attribute. Give you select element a name value and it will work.
表单适用于名称,而不是ID属性。为您选择元素一个名称值,它将起作用。
#2
1
Your code seems to correct but you have a mistake. Look at select
, it has no attribute name
. So, $form.serialize()
will not work on this element.
你的代码似乎是正确的,但你有一个错误。看看select,它没有属性名称。因此,$ form.serialize()将不适用于此元素。
This is fixed by add attribute name to select
通过添加属性名称来修复此问题
<form id='foo'>
<p>
<label>Message</label>
<textarea id='message' name='message' rows='5'></textarea>
</p>
<br/>
<p>
<label>Name</label>
<select name="test">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
</p><br/>
<div id='success'></div>
<button type='submit'>Send</button>
</form>
<p id="showdata">
</p>
More information : Form Serialize
更多信息:表单序列化
#3
0
If you use $form.serialize()
in select element you should add attribute name in there because $form.serialize()
will get values from attribute name
如果在select元素中使用$ form.serialize(),则应在其中添加属性名称,因为$ form.serialize()将从属性名称中获取值
Update
https://jsfiddle.net/drhe1L9u/
Add attribute name to select element
添加属性名称以选择元素