modify serialize form function to take input ID instead of name

I have a JavaScript function which iterates over a form to obtain input values based on the name of the input field. However, in order to satisfy API post request parameters, I need the input in the following JSON format:

{
    "name": "mobile offers",
     "group_id": "35"
}

where the "name" field is the result of input from a textbox and the "group_id" field is the result of the selection of an option via checkbox. How can I change this function so that it takes the input based on name for the textbox field and id for the checkboxes?

$.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;
};

I've tried doing it using two separate functions, one for name and one for id, but cant figure out how to put the output together and it doesn't make sense to serialize the form twice.

Answers


If name of checkboxes is incorrect, can simply change them to value of id before serializing

$(':checkbox').attr('name',function(){
 return this.id;
});
 /* now serialize form*/

Need Your Help

Best way to implement a stored procedure with full text search

sql sql-server stored-procedures full-text-search dynamic-sql

I would like to run a search with MSSQL Full text engine where given the following user input:

Finding all radio buttons in 'unknown' form

javascript input tags radio

I am trying to write javascript code that will find all radio groups in a form and check to make sure one is clicked in each group. This is not hard if you know the form and group names. What I w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.