How can I get form data with JavaScript/jQuery?

前端 未结 28 1930
不知归路
不知归路 2020-11-22 12:39

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?

For example:



        
28条回答
  •  醉酒成梦
    2020-11-22 13:33

    Here is a working JavaScript only implementation which correctly handles checkboxes, radio buttons, and sliders (probably other input types as well, but I've only tested these).

    function setOrPush(target, val) {
        var result = val;
        if (target) {
            result = [target];
            result.push(val);
        }
        return result;
    }
    
    function getFormResults(formElement) {
        var formElements = formElement.elements;
        var formParams = {};
        var i = 0;
        var elem = null;
        for (i = 0; i < formElements.length; i += 1) {
            elem = formElements[i];
            switch (elem.type) {
                case 'submit':
                    break;
                case 'radio':
                    if (elem.checked) {
                        formParams[elem.name] = elem.value;
                    }
                    break;
                case 'checkbox':
                    if (elem.checked) {
                        formParams[elem.name] = setOrPush(formParams[elem.name], elem.value);
                    }
                    break;
                default:
                    formParams[elem.name] = setOrPush(formParams[elem.name], elem.value);
            }
        }
        return formParams;
    }
    

    Working example:

        function setOrPush(target, val) {
          var result = val;
          if (target) {
            result = [target];
            result.push(val);
          }
          return result;
        }
    
        function getFormResults(formElement) {
          var formElements = formElement.elements;
          var formParams = {};
          var i = 0;
          var elem = null;
          for (i = 0; i < formElements.length; i += 1) {
            elem = formElements[i];
            switch (elem.type) {
              case 'submit':
                break;
              case 'radio':
                if (elem.checked) {
                  formParams[elem.name] = elem.value;
                }
                break;
              case 'checkbox':
                if (elem.checked) {
                  formParams[elem.name] = setOrPush(formParams[elem.name], elem.value);
                }
                break;
              default:
                formParams[elem.name] = setOrPush(formParams[elem.name], elem.value);
            }
          }
          return formParams;
        }
    
        //
        // Boilerplate for running the snippet/form
        //
    
        function ok() {
          var params = getFormResults(document.getElementById('main_form'));
          document.getElementById('results_wrapper').innerHTML = JSON.stringify(params, null, ' ');
        }
    
        (function() {
          var main_form = document.getElementById('main_form');
          main_form.addEventListener('submit', function(event) {
            event.preventDefault();
            ok();
          }, false);
        })();

    what is a?

    A and/or B?

    edit:

    If you're looking for a more complete implementation, then take a look at this section of the project I made this for. I'll update this question eventually with the complete solution I came up with, but maybe this will be helpful to someone.

提交回复
热议问题