admin管理员组

文章数量:1287893

Hay guys, I'm doing some work where i have a large collection of forms. Seems these aren't 'posted' the traditional way (i.e using a submit button). I need a way to collect all the names and values of all form data when clicking a link (document.location is then used for redirects).

I want names to stay intact so that i can used a simple PHP script to work with the data.

any suggestions?

Hay guys, I'm doing some work where i have a large collection of forms. Seems these aren't 'posted' the traditional way (i.e using a submit button). I need a way to collect all the names and values of all form data when clicking a link (document.location is then used for redirects).

I want names to stay intact so that i can used a simple PHP script to work with the data.

any suggestions?

Share Improve this question edited Nov 30, 2009 at 13:20 AnthonyWJones 190k35 gold badges235 silver badges307 bronze badges asked Nov 24, 2009 at 12:55 dottydotty 41.5k66 gold badges153 silver badges197 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

Might I suggest Serialize Form to JSON:

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};

and then you can do:

var formArray = $("#myform").serializeObject();
$.fn.valuesArr = function()
{
    var a = [];
    jQuery.each(this, function(i, field){
        a.push($.trim(field.value));
    });
    return a;
}

USE:

var myArr = $('input', $parentJQelem).valuesArr();

You could use something like my submit helper as a base

function submit(form) {
  var form = $(form);
  $.ajax(
    { data: $.param( form.serializeArray()),
      dataType:'script',
      type:'post',
      url: form.attr("action")});
}

instead of a 'serializeArray()' i would use 'serialize()' and instead of posting it using ajax you could do whatever you want.

Sorry, dont have more time right now, but hopefully this snippet helps you.

cletus's answer is the best one in terms of efficency.

This is however another working solution that does not rely on JSON:

//this is my object I will fill my array with
function myObject(name, value)
{
    this.name = name;
    this.value = value;
}

var arr = $.map(
    $('span'), function (item) {
       var $item = $(item); //no need to $(item) 2 times
       return new 
         myObject(
           $item.attr("name"),     //name field
           $item.text()            //value field
         );
    }
);

arr will be an array of myObject, each of them containing a name property and a value property.

Use your selector instead of $('span').

but all this functions dont work witch array names in inputs.

example -

this is correct for submit post form but when serialize i get

form[type[]]:2

this is not correct - i need - form[type][]:2

本文标签: phpJquery get all input values and create an array (with names)Stack Overflow