lakshay lakshay - 3 months ago 11
jQuery Question

How to serialize a form using jquery .serialize() method?

My Form is:

<form class="something" id="main-form">
-----content----




My Jquery is:

$("form :input").change(function() {
var data = $('#main-form').serialize();
console.log(data);


$.ajax({
url:"<?php echo Yii::app()->createAbsoluteUrl('jobs/index'); ?>",
//dataType:"json",
type: "POST",
data: $('#main-form').serialize(),
success: function(data){
}
});
});


But the form is not serialized.The data field is coming empty.Please help.

Answer

From .serialize(); :

the element must have a name attribute.

So make sure you give all inputs a name='whatever'.

Here's a demo: http://jsfiddle.net/CWJDj/1/

Comments