最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Post data of all inputs with jQuery? - Stack Overflow

programmeradmin6浏览0评论

I do generate some <input>s with random names on the page (nearly ten at once).

Stuck on a question about submitting them.

I use this code to submit:

$('.table_data').submit(function(event)
{
    event.preventDefault();
    // some validation check and then

    $.post(url, {user_mail: mail}, function()
    {
        alert('done);
    });
});

The problem is, it submits user_mail only.

How do I submit data of all <input>s?

Have tried to use $.post(url, function(){}), it sends nothing.

Its possible to get all the inputs into array and then throw to the $.post function, but I'm not sure how to do it well.

I do generate some <input>s with random names on the page (nearly ten at once).

Stuck on a question about submitting them.

I use this code to submit:

$('.table_data').submit(function(event)
{
    event.preventDefault();
    // some validation check and then

    $.post(url, {user_mail: mail}, function()
    {
        alert('done);
    });
});

The problem is, it submits user_mail only.

How do I submit data of all <input>s?

Have tried to use $.post(url, function(){}), it sends nothing.

Its possible to get all the inputs into array and then throw to the $.post function, but I'm not sure how to do it well.

Share Improve this question asked Jun 3, 2012 at 18:45 JamesJames 43.7k54 gold badges137 silver badges163 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

If your form's classname is table_data then use this

$.post(url,$('.table_data').serialize(), function(){})

And you will get form input's names as key of your global POST array.

Full code

$('.table_data').submit(function(event)
{
    event.preventDefault();
    // some validation check and then

    $.post(url, $(this).serialize(), function()
    {
        alert('done');
    });
});

Read more on $.serialize

$.post(
   url,                  // url to submit
   $(this).serialize(),  // make a standard-looking query string using all inputs value
   function(response) {        
     // success function
     // in parameter response is to capture data send from capture
  },
  'json'  // dataTyep in which format you're accepting 
          // data from server may be html, xml ans so on
);

Related refs:

  • $.serialize()

  • $.post()

You are preventing the default behaviour on form submit using event.preventDefault() and then only sending the usermail value to server using ajax.

From what I understand, you need to serialize the form so that you can obtain all the names and their values and then send it to server. For that you need serialize function in jQuery

发布评论

评论列表(0)

  1. 暂无评论