[英]Get all form values into javascript array
I am attempting to get all the form values into a normal array[]. 我正在尝试将所有表单值放入普通的array []中。 I had it working for tags but then I added some tags and can't seem to get it.
我已经将它用于标签,但是后来我添加了一些标签,但似乎无法获取它。
With just tags it worked with this 仅使用标签即可
var content = document.querySelectorAll("#form input[name='content[]']");
I am currently trying something like this 我目前正在尝试这样的事情
var content = document.elements["content[]"].value;
This form can change from user input down the road as each section of the form is a module that they choose to add. 表单可以从用户的输入中更改,因为表单的每个部分都是他们选择添加的模块。 It is also important to get the values in order, if that isn't possible then I would need to make it a JSON array.
按顺序获取值也很重要,如果不可能的话,则需要将其设为JSON数组。 Pure javascript or jquery is fine either way.
纯JavaScript或jquery都可以。
Thank you for any help. 感谢您的任何帮助。
EDIT 编辑
I used this to solve my problem 我用这个来解决我的问题
var contents=[]
var content = $('#form').serializeArray()
for (var i = 0; i < content.length; i++) {
contents[contents.length]=content[i].value
};
Try 尝试
html HTML
<form id="form">
<input type="text" name="content[]" value="abc" />
<textarea name="textarea" value="">123</textarea>
</form>
js JS
$(function() {
var form = $("#form");
// escape `[]`
var content = form.find("input[name=content\\[\\]]");
// array `literal declaration`
var _arr = [content.prop("value")];
// map all form values to single array
var arr = $.map(form.serializeArray(), function(v, k) {
return [v.value]
});
// array literal with `textarea` `value`
var t = [$("textarea").prop("value")];
console.log(_arr, arr, t);
// _arr: `["abc"]` , arr:`["abc", "123"]` t:`["123"]`
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.