Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialize from a table element and not the whole form

Trying to serialize just the elements from a specific table but it only returns a result if i do the whole Form

in the below code, i want to ajax just the elements in tbl2

<form>
 <input type="text" id="tb1" name="tbl1"/>
  <table name="tbl1">
   <tr><td><input type="text" name="tb2"/></td></tr>
 </table>
 <table name="tbl2">
   <tr><td><input type="text" name="tb3"/></td></tr>
   <tr><td><input type="text" name="tb4"/></td></tr>
 </table>
</form>

the code

var params = $("#tbl2").serialize();

var resp = $.ajax({
    async: false,
    type: "POST",
    url: AppRoot + "webhandlers/postback.ashx",
    data: params
});
like image 392
Christian Avatar asked Sep 28 '11 10:09

Christian


People also ask

How do you serialize form data?

To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.

What is data $(' form ') serialize ()?

The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <input> , <textarea> , and <select> : $( "input, textarea, select" ).serialize();

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .

What does serialize function do?

Definition and Usage The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.


3 Answers

First and foremost, a <table> cannot have a name attribute, and even if it could, the jQuery ID selector (#) would not match it.

If you use id instead (<table id="tbl2">), it will work like this:

var params = $("#tbl2 :input").serialize();

The :input selector selects all the form elements (here, inside #tbl2), it is needed because serialize() will only work on those.

Please also check out my jsFiddle Demo.

like image 119
kapa Avatar answered Sep 30 '22 18:09

kapa


you cannot serialize a table -- that method doesn't apply to that kind of DOM object, only forms and form fields can be serialized.

if you really want to do what you're proposing, you need the proper selector to pick just the children of tbl2 that are also form elements, and then you'll have to serialize each one of those by hand. someone did it in another question, here: serialize without a form?

a better way might be to disable all the form elements that are NOT in the table you're interested in -- you'll need a selector to pick all form elements that are not child elements of tbl2 -- and THEN serialize the form. the disabled elements will be omitted.

like image 44
Igor Serebryany Avatar answered Sep 30 '22 18:09

Igor Serebryany


you can use the serializeArray method, which will give you the array of input fields and can be used with the data.

var params = $("#tbl2 input").serializeArray();
like image 27
Jayendra Avatar answered Sep 30 '22 17:09

Jayendra