Search code examples
javascriptjquerypostjquery-post

How to serialize multiple checkbox values by jQuery?


I modified the simple example of jQuery.post as

  $("#searchForm").submit(function(event) {
    event.preventDefault(); 
    var $form = $( this ),
        term = $( "input[name^=tick]:checked" ).serialize(),
        url = $form.attr( 'action' );
    $.post( url, { ticks: term, id: '55' },
      function( data ) {
          $( "#result" ).empty().append( data );
      }
    );
  });

This works for single checkbox with val() but not for multiple checkboxes in

<input type="checkbox" name="tick" value="'.$value.'" />

since serialize() should generateticks: termto be used astermin$.post`.

How can I make the serialize() to generate appropriate data for $.post

NOTE: I do not want to serialize the entire form but only checked values of checkbox INPUT.


Solution

  • You could use .serializeArray() Ref: http://api.jquery.com/serializeArray/