lewis4u lewis4u - 1 month ago 4x
Ajax Question

jQuery ajax serialize only 2 fields

I know how to serialize the whole form like in example below or one specific field of the form by just changing the line:

data: $('form').serialize(),


data: $('#input-field').serialize(),


$(document).on('input paste', '#soap', function () {
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: $('form').serialize(),

But i want to serialize 2 input fields that have an id....how to do that?


Way 1: By comma separated selector you can serialize two fields together using following code

var seializedTwoFields = $('#input-field1,#input-field2').serialize();

Way 2: When you use the jQuery serialize() function, it simply turns your field into a string in the format a=1. So you can certainly apply this function to two fields and concatenate the result, with an & between them.

Please try this snippet.

var formfield1 = $('#input-field1').serialize();
var formfield2 = $('#input-field2').serialize();

var seializedTwoFields = formfield1+'&'+formfield2;