如何从 HTML 表单中获取 key/value 数据集

How to get a key/value data set from a HTML form

我只是在寻找一种方法来 <form>.

中获取所有值

我在网上搜索了一会儿,偶然发现 FormData,这似乎正是我要找的东西。

但是它的 API 在任何浏览器上都是 not available,所以我需要一个替代方案。


在我的特定情况下,我需要的是 key/value 对的对象。例如:

<form>
  <input type="text" name="firstname" value="John" />
  <input type="text" name="surname" value="doe" />
  <input type="email" name="email" value="" />
  <input type="radio" name="gender" value="male" />
  <input type="radio" name="gender" value="female" />
</form>

对象应该是:

{
  firstname: "John",
  surname: "doe",
  email: "",
  gender: ""
}

编辑: 以上只是一个示例,它不仅适用于 <input>,还适用于其他标签(例如 <select><textarea> 等等...甚至应该支持 <input type="file">)。

您需要手动生成 json/javascript 对象,然后再将其发送到服务器。 应该是一个名为 onSubmit 的方法,它会选择每个表单的输入值并创建一个对象,然后将其发送到服务器页面。

您可以参考这个与您正在寻找的答案相似的答案: Convert form data to JavaScript object with jQuery

您可以选择手动方式(但以下代码未经过测试);

var form = document.getElementsByTagName("form");
var inputs = form[0].getElementsByTagName("input");

var formData = {};
for(var i=0; i< inputs.length; i++){
   formData[inputs[i].name] = inputs[i].value;
}
var formdata = JSON.stringify(formData);

如果您使用图书馆,这会更容易。 例如:- 在 jQuery:

 var formObjects = $("form :input");
 formObjects.each(
function(){
formData[$(this).attr("name")] = $(this).val(); /*setting up name/value pairs */ 

 }
  );

在此代码中formObjects包含所有input,select和textarea等表单元素标签。所以我们不需要在纯 JS 中手动搜索每个 like。除了单选按钮(正如@enhzflep 所暗示的,这对 input[type=radio] 不起作用)

但是如果你使用jQuery你可以直接使用jQuery的serialize()函数通过名称-值对获取整个表单。

var url_friendly_name_value_string = $("form").serialize();

只需使用jQuery。

HTML:

<form id="my-form">
  <input type="text" name="my-field" />
</form>

JS:

var data = $('form#my-form').serializeArray(); // [{'my-field': 'value'}]
$.ajax({
  data: data
  //other config goes here
});

请原谅我没有格式化示例,但它很好理解。单击 'Get All Values' 以在警报中查看 key/value 对。

JS Fiddle

$(function(){
    $(".getFormVal").on("click",function(e){
        e.preventDefault();
        var formObj={};
        var formEle = $(".form").find("input:not([type=submit],[type=button]),select,textarea");

        $(formEle).each(function(){
            if($(this).prop("tagName").toLowerCase() == "input"){                           
                if( ($(this).attr("type").toLowerCase() == "text") || 
                   ($(this).attr("type").toLowerCase() == "radio" && $(this).is(":checked")) || 
                   ($(this).attr("type").toLowerCase() == "file") ){
                    formObj[$(this).attr("name")] = $(this).val();                               
                }else if( $(this).attr("type").toLowerCase() == "checkbox" && $(this).is(":checked") ){
                    if(formObj[$(this).attr("name")] === undefined){
                        formObj[$(this).attr("name")] = [];
                    }
                    formObj[$(this).attr("name")].push($(this).val());
                }
            }else if( $(this).prop("tagName").toLowerCase() == "textarea" ){
                formObj[$(this).attr("name")] = $(this).val();
            }else if( $(this).prop("tagName").toLowerCase() == "select" ){
                if($(this).attr("multiple")){
                    if(formObj[$(this).attr("name")] === undefined){
                        var selectEleName = $(this).attr("name");
                        formObj[selectEleName] = [];
                    }                               
                    $('option:selected',this).each(function(i, selected){
                        formObj[selectEleName].push($(this).attr("value")); 
                    });

                }else{
                    formObj[$(this).attr("name")] = $(this).val();
                }
            }

        });

        alert(JSON.stringify(formObj));
    }); 
});

我建议您使用 css selectors for selecting the inputs from your form: querySelector and querySelectorAll。 这是 working code,这是 js 代码。

function get_form_data()
{
    var el = document.querySelector('form');
    var matches = el.querySelectorAll('input[name]');

    var data = {};
    for(var i=0; i< matches.length; i++){
       data[matches[i].name] = matches[i].value;
    }
    var json_data = JSON.stringify(data);
}

当然,您可以根据需要修改选择器。 我希望这对你有用,祝你好运。

当我最初写这个答案时 FormData 没有得到广泛支持(问题中明确指出了这一点)。现在已经 6 年了,FormData has excellent cross-browser support.

正因为如此,我强烈推荐使用FormData直接访问数据或序列化数据到服务器。

Jake Archibald has an excellent post describing FormData (and URLSearchParams) in depth,我不会在这里重现,但我会包含一些相关的片段:

You can populate FormData state directly:

const formData = new FormData();
formData.set('foo', 'bar');
formData.set('hello', 'world');

...you can read an HTML form directly as FormData:

const formElement = document.querySelector('form');
const formData = new FormData(formElement);
console.log(formData.get('username'));

...you can use FormData directly as a fetch body:

const formData = new FormData();
formData.set('foo', 'bar');
formData.set('hello', 'world');

fetch(url, {
  method: 'POST',
  body: formData,
});

我建议完整阅读 Jake 的 post 并使用浏览器随附的 API 而不是添加更多代码来构建同一事物的弹性较低的版本。


我的原创 post 保存下来 post为了真实 :

如果没有对边缘情况应该发生什么以及需要什么级别的浏览器支持的明确定义,很难对这个问题给出一个完美的答案。

有很多容易遗漏的表单行为,这就是为什么我建议使用库中维护良好的函数,例如 jQuery's serializeArray():

$('form').serializeArray();

我知道最近有一个很大的推动力要摆脱不必要的,包括 jQuery,所以对于那些对普通 JS 解决方案感兴趣的人来说 serializeArray 不会做。

下一个困难来自确定需要什么级别的浏览器支持。 HTMLFormElement.elements 显着简化了序列化实现,没有它选择表单相关元素是一件很痛苦的事情。

考虑:

<form id="example">...</form>
<input type="text" form="example" name="lorem" value="ipsum"/>

符合规范的实现需要包含 input 元素。我会假设我 可以 使用它,并将填充它作为练习留给 reader.

在那之后,不清楚应该如何支持 <input type="file"/>。我不热衷于将文件元素不必要地序列化为一个字符串,所以我假设序列化将是输入的名称和值,即使该值实际上没有用。

最后,输入结构为:

{
    'input name': 'value',
    'textarea name': 'value'
}

过于天真,因为它没有考虑 <select multiple> 元素,或者两个输入具有相同名称的情况。我假设输入会更好:

[
    {
        name: 'input name',
        value: 'value'
    },
    {
        name: 'textarea name',
        value: 'value'
    }
]

...再次将其转换为不同的结构作为 reader.

的练习

已经给我代码了!

var serialize = (function (slice) {
    return function (form) {
        //no form, no serialization
        if (form == null)
            return null;

        //get the form elements and convert to an array
        return slice.call(form.elements)
            .filter(function (element) {
                //remove disabled elements
                return !element.disabled;
            }).filter(function (element) {
                //remove unchecked checkboxes and radio buttons
                return !/^input$/i.test(element.tagName) || !/^(?:checkbox|radio)$/i.test(element.type) || element.checked;
            }).filter(function (element) {
                //remove <select multiple> elements with no values selected
                return !/^select$/i.test(element.tagName) || element.selectedOptions.length > 0;
            }).map(function (element) {
                switch (element.tagName.toLowerCase()) {
                    case 'checkbox':
                    case 'radio':
                        return {
                            name: element.name,
                            value: element.value === null ? 'on' : element.value
                        };
                    case 'select':
                        if (element.multiple) {
                            return {
                                name: element.name,
                                value: slice.call(element.selectedOptions)
                                    .map(function (option) {
                                        return option.value;
                                    })
                            };
                        }
                        return {
                            name: element.name,
                            value: element.value
                        };
                    default:
                        return {
                            name: element.name,
                            value: element.value || ''
                        };
                }
            });
    }
}(Array.prototype.slice));

这是纯粹的 javascript 解决方案,它以包括文件的形式为所有类型的输入提供所需的输出。

示例输出

{
  "firstname":"John",
  "surname":"doe", 
  "email":"abc@gmail.com",
  "gender":"female", 
  "subscribe":true, 
  "selection":"M",
  "description":"sdfs sdfs sdf",
  "upload":"data:text/plain;base64,/v8JLAk+CTMAIAkVCUcJNgk1ACAJIAk+CRUJMAlHAC4ADQAKAA0ACgk5CT8JAgkoCU0JJglBCSQJTQk1CT4JGgk+ACAJHwkjCSQJTQkVCT4JMAAuACAJNQlNCS8JAgkXCRoJPwkkCU0JMAkVCT4JMAAgCSQJRwAgCTkJPwkoCU0JJglBCTkJTQkwCUEJJgkvCTgJLglNCTAJPgkfAA0ACgkkCUsJAgkhACAJNQk+CRwJNQlBCSgAIAkoCU0JLwk+CS8AIAkuCT8JMwkkACAJKAk4CUcJMgAgCSQJMAANAAoJJAlLCQIJIQk+CSQAIAk1CT4JHAk1CUEJKAAgCSgJTQkvCT4JLwAgCS4JPwkzCTUJPgAsAA0ACgkqCSMAIAkoCU0JLwk+CS8AIAk5CT4AIAkdCT4JMgk+CRoAIAkqCT4JOQk/CRwJRwAuAA0ACgkPCRUAIAkuCT4JIwlBCTgAIAkkCUEJLgkaCU0JLwk+ACAJLgk+CR0JTQkvCT4AIAk4CT4JMAkWCT4JGgAsACAJCQkCCRoJQAkoCUcAIAkGCSMJPwAgCTUJHAkoCT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m+']');
    var inputs = el.querySelectorAll('input,select,textarea');
    
    var data = {};
    for(var i=0; i< inputs.length; i++){
      switch(inputs[i].type){
          case 'file':
                      var file = inputs[i].files[0];
                      if(file){
                        var oReader = new FileReader();
                        (function(i){
                          oReader.onload = function(e){
                            data[inputs[i].name] = e.target.result;
                            alert(JSON.stringify(data));
                            document.querySelector('p').innerHTML = JSON.stringify(data);
                            console.dir(data);
                      };
                      oReader.readAsDataURL(file); 
                      })(i)
                      }
                     
                      break;
          case 'checkbox':     
                      data[inputs[i].name] = inputs[i].checked;
                      break;
          default:
                      data[inputs[i].name] = inputs[i].value;
      
      }
    }
    document.querySelector('p').innerHTML = JSON.stringify(data);
  }
form{width: 400px}
<form name="jsonForm">
  <input type="text" name="firstname" value="John" />
  <input type="text" name="surname" value="doe" />
  <input type="email" name="email" value="abc@gmail.com" />
 M: <input type="radio" name="gender" value="male" checked/>
 F: <input type="radio" name="gender" value="female" />
 subscribe: <input type="checkbox" name="subscribe" checked/>
  <input type="file" name="upload" />
  <select name="selection"><option value=M>M</option><option value=J>J</option></select>
  <textarea name="description"></textarea>
</form>
<hr/>
<input type=button value="getData" onclick=getData('jsonForm')>
<p><p>

聚会迟到了,但我是这样做的:

(function getFormData(){
    var form, inputs, formData = {}, outputdiv;
    (function init(){
        outputdiv = document.getElementById("output");
        output();
    })();  
    
    function build(){
        form = document.querySelector("form"),
        inputs = form.querySelectorAll("input, select, textarea"),
        formData = {},
        arr = [];
        for(var i=0; i< inputs.length; i++){
            var input = inputs[i];
            input.onchange  = function(){output();};
            if(input.type=='radio'){
                if(input.checked){
                   arr.push(input.name,input.value);
                }
            } else if(input.type=='checkbox'){                
                if(input.checked){
                    arr.push(input.name,input.value);
                }
            } else if(input.multiple){                
                //find each selected child
                var options = input && input.options;
                for (var o=0; o<options.length; o++) {
                    var opt = options[o],
                        val = (opt.value || opt.text)
                    if (opt.selected) {
                        arr.push(input.name,val);
                    }
                }
            } else {
                arr.push(input.name,input.value);
            }            
        }
        var formdata = JSON.stringify(arr);
        return formdata;
    }
    function output(){
       outputdiv.innerHTML = build();
    }
    
})()
<form>
  <input type="text" name="text1" value="" />
  <input type="text" name="text2" value="" />
  <input type="email" name="text3" value="" />
    <br />
  <input type="radio" name="radio" value="radio1" />
  <input type="radio" name="radio" value="radio2" />
  <input type="radio" name="radio" value="radio3" />
    <br />
  <input type="checkbox" name="checkboxes" value="checkbox1" />
  <input type="checkbox" name="checkboxes" value="checkbox2" />
  <input type="checkbox" name="checkboxes" value="checkbox3" />
    <br />
    <textarea name="textarea"></textarea>
    <br />
    <select name="option">
        <option value="">options</option>
        <option>option text</option>
        <option value="option 2 value">option 2 text</option>
        <option>option 3 text</option>
    </select>
  <br />
    <select multiple="true" name="options">
        <option>opt multi 1</option>
        <option value="opt multi 2 value">opt multi 2</option>
        <option>opt multi 3</option>
    </select>
</form>
<div id="output"></div>

如果您需要 {key: value, ...}

的对象
const form = document.getElementById("your-form")

const formEntries = new FormData(form).entries();
const formData = Object.assign(...Array.from(formEntries, ([name, value]) => ({[name]: value})));

console.log(formData)