I have a JSP page with a form. When I submit the form, it build and gerenates a JSON string that I do an AJAX post with.
I have a problem though, I need to get multiple values out of the form,and I am using the following to do this:
.find('input[name=item1])
.not('input[type=hidden]')
How can I do this to get inputs with names item1, item2 and item3?
I tried this but it didn’t work?
.find('input[name=item1][name=item2][name=item3]')
Below is my code for this:
// Create JSON based data object
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
// Form Submission
$('#form').submit( function() {
// Create data array, used for building request message
var data = {
request: {
requestType: "request",
fields: [ {
itemX1 : '1',
itemX2 : '2',
itemX3 : '3'
} ]
}
};
// Create field array based variables for request message
var fields = {
fields: [ {
itemX1 : null,
itemX2 : null,
itemX3 : null
} ]
};
// Get reqired data from the form submitted
fields = $('#form')
//.find('input')
.find('input[name=lmBtId][name=my]')
.not('input[type=hidden]')
.serializeObject();
// Set the field array variables with data
fields.itemX1 = '1';
fields.itemX2 = '2';
fields.itemX3 = '3';
// Update data array with newly updated field array
data.request.fields = [fields];
var finalData = JSON.stringify(data);
This page will provide information on selectors,
http://api.jquery.com/multiple-selector/
Please try this
And do something for each of them add,