[英]serializeArray() gives empty array
I'm trying to use .seralizeArray()
to harvest form inputs values. 我正在尝试使用
.seralizeArray()
来获取表单输入值。
The input fields consist of a list of item with respective corresponding prices; 输入字段包含具有相应价格的项目列表; I would like to save each value against its corresponding key pair but keep getting an error:
... empty array with 0 length
. 我想保存每个值对应其对应的密钥对,但不断收到错误:
... empty array with 0 length
。
I've tried a few combinations of selectors but still get []
. 我尝试了一些选择器的组合,但仍然得到
[]
。
How can I serialize successfully? 如何成功序列化?
Below is my code: 以下是我的代码:
HTML HTML
<fieldset id='itemInformation'>
<h2>Items</h2>
<div class="itemGroup">
<input type="text" id="item1Name" value="Item 1" class="itemNames">
<input type="number" step="0.01" id="item1Price" value="0.00">
</div>
<div class="itemGroup">
<input type="text" id="item2Name" value="Item 2" class="itemNames">
<input type="number" step="0.01" id="item2Price" value="0.00">
</div>
<div class="itemGroup">
<input type="text" id="item3Name" value="Item 3" class="itemNames">
<input type="number" step="0.01" id="item3Price" value="0.00">
</div>
<div class="changeNumber">
<button class="increase">+</button>
<button class="decrease">–</button>
</div>
<div class="buttons">
<button class="previousButton btn">Previous</button>
<button class="nextButton btn">Next</button>
</div>
</fieldset>
JS JS
$('#itemInformation .nextButton').bind('click', function (event) {
event.preventDefault()
console.log($('.itemGroup').serializeArray())
})
Heads up! 当心!
You need to add a name
attribute to all your <input>
fields as in: 您需要为所有
<input>
字段添加name
属性,如下所示:
<input type="text" id="item1Name" value="Item 1" class="itemNames" name="itemNames">
Also, use <form>
instead of <fieldset>
unless you do have explicit reason for such a purpose. 此外,使用
<form>
而不是<fieldset>
除非您确实有明确的理由这样做。
The syntax for serializeArray();
serializeArray();
的语法serializeArray();
is as follow: $(selector).serializeArray();
如下:
$(selector).serializeArray();
. 。
Reference: w3schools (read on Definition and Usage ) . 参考: w3schools (阅读定义和用法) 。
Same concept used here with the intention to serialize a collection of alike item . 这里使用相同的概念,旨在序列化相似项目的集合 。
Please note : using of the <form>
element is not compulsory but simply recommended to get you easily and effectively serialize based on your intended purpose and information shared above in your post; 请注意 :使用
<form>
元素不是强制性的,但只是建议您根据您在帖子中分享的预期目的和信息轻松有效地序列化; I might have misread you on that, but the most important thing missing out was the name
attribute, which accounts for your empty array []
as there's nothing available to be serialized. 我可能误读了你,但最重要的是错过了
name
属性,它解释了你的空数组[]
因为没有什么可以序列化。
Remember that based on your project requirements, you may have to make use of <fieldset>
as and when required. 请记住,根据您的项目要求,您可能必须在需要时使用
<fieldset>
。
You may use the <fieldset>
element with different id s to group related items you want to identify as a unit. 您可以使用具有不同ID的
<fieldset>
元素将要标识的相关项目分组为一个单元。 As such, you may have something like this: 因此,您可能会遇到以下情况:
<form id="itemDetails">
<fieldset id="itemInformation">
<!-- itemInformation label and input fields -->
</fieldset>
<fieldset id="itemOrigin">
<!-- itemOrigin label and input fields -->
</fieldset>
<fieldset id="itemDestination">
<!-- itemDestination label and input fields -->
</fieldset>
</form>
You can then use $('#itemDetails').serializeArray();
然后你可以使用
$('#itemDetails').serializeArray();
to serialize your entire form or $('#FIELDSET_ID').serializeArray();
序列化整个表单或
$('#FIELDSET_ID').serializeArray();
where FIELDSET_ID is the corresponding id of a selected fieldset you want to serialize out of your form based on events so as to achieve your intended purpose. 其中FIELDSET_ID是您要根据事件从表单序列化所选字段集的相应ID ,以实现您的预期目的。
Added the snippet below to illustrate how you can effectively pair up an item with it's corresponding value; 添加了下面的代码段,说明如何有效地将项目与其对应的值配对;
$('#itemInformation .nextButton').bind('click', function (event) { event.preventDefault(); console.table($('#itemInformation').serializeArray()); console.log($('#itemInformation').serializeArray()); });
<form id="itemDetails"> <!-- grouping all fieldset under a form element with an assigned id where they can all be hierarchically referenced for precision where needed --> <fieldset id="itemInformation"> <h2>Items</h2> <div class="itemGroup"> <input type="number" id="item1Name" placeholder="0.00" class="itemNames" name="item1Name"> </div> <div class="itemGroup"> <input type="number" id="item2Name" placeholder="0.00" class="itemNames" name="item2Name"> </div> <div class="itemGroup"> <input type="number" id="item3Name" placeholder="0.00" class="itemNames" name="item3Name"> </div> <div class="changeNumber"> <button class="increase">+</button> <button class="decrease">–</button> </div> <div class="buttons"> <button class="previousButton btn">Previous</button> <button class="nextButton btn">Next</button> </div> </fieldset> <!-- other fieldset as necessary and harvest their input separately in a similar fashion as done with the first one itemInformation --> <fieldset id="itemOrigin"> <!-- itemOrigin label and input fields --> </fieldset> <fieldset id="itemDestination"> <!-- itemDestination label and input fields --> </fieldset> </form> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Below, a sample output of the above in a browser developer console (here: Google Chrome Browser); 下面是浏览器开发者控制台中的上述示例输出(此处为:Google Chrome浏览器);
Also, available here (JSfiddle) . 此外, 这里可用(JSfiddle) 。
You will realise that each <div class="itemGroup">
has a single <input>
element which has no value
attribute but a placeholder
to illustrate the expected input format. 您将意识到每个
<div class="itemGroup">
都有一个<input>
元素,它没有 value
属性,但是一个placeholder
来说明预期的输入格式。 You may use value
instead in case you want to pre-define a fixed value to be paired up against a corresponding item should there be no value entered. 如果您没有输入
value
则可以使用value
来预先定义要与相应项目配对的固定值。 The name
attribute is necessary: do not omit it. name
属性是必需的:不要省略它。
As such (as above), when you serialize, the values collected from your input field will be paired up with the corresponding entered value and or default one(s). 因此(如上所述),序列化时,从输入字段收集的值将与相应的输入值和/或默认值一起配对。
The .serializeArray()
method uses the standard W3C rules for successful controls to determine which elements it should include; .serializeArray()
方法使用标准W3C规则来成功控制,以确定它应包含哪些元素; in particular the element cannot be disabled and must contain a name attribute. 特别是该元素不能被禁用,并且必须包含name属性。
IMO, Use form
and apply name
attributes to all the inputs. IMO,使用
form
并将name
属性应用于所有输入。
var formdata = $( "#itemInformation" ).serializeArray();
FYI, use form
is a good practice but it's not compulsory to use that. 仅供参考,使用
form
是一种很好的做法,但使用它并不是强制性的。
Official documentation serializeArray() 官方文档serializeArray()
$('#itemInformation .nextButton').bind('click', function (event) { event.preventDefault() console.log($('#itemInformation').serializeArray()) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id='itemInformation'> <h2>Items</h2> <div class="itemGroup"> <input type="text" id="item1Name" name="item1Name" value="Item 1" class="itemNames"> <input type="number" step="0.01" name="item1Price" id="item1Price" value="0.00"> </div> <div class="itemGroup"> <input type="text" id="item2Name" name="item2Name" value="Item 2" class="itemNames"> <input type="number" step="0.01" name="item2Price" id="item2Price" value="0.00"> </div> <div class="itemGroup"> <input type="text" name="item3Name" id="item3Name" value="Item 3" class="itemNames"> <input type="number" step="0.01" name"item3Price" id="item3Price" value="0.00"> </div> <div class="changeNumber"> <button class="increase">+</button> <button class="decrease">–</button> </div> <div class="buttons"> <button class="previousButton btn">Previous</button> <button class="nextButton btn">Next</button> </div> </fieldset>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.