jQuery - The serialize function


The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <input>, <textarea>, and <select>: $( “input, textarea, select” ).serialize();
<div id="demo-edit-dialog" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Edit Project</h4>
            <div class="modal-body">
                <form id="demo-edit-form" action='/demo/edit-project' method='post'>
                    <input id="project-id" type="hidden" value="PRJ-DEMO-001">
                    <div class="form-group">
                        <label for="project_name">Project Name</label>
                        <input class="form-control" name="project_name" type="text" value="">
                    <div class="form-group">
                        <label for="project_desc">Description</label>
                        <input class="form-control" name="project_desc" type="text" value="">
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary modal-submit">Save changes</button>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

    function show_edit_dialog(demoObject) {
        if (typeof (demoObject) !== undefined) {
            if (typeof (demoObject.name) !== undefined) {
                $('#demo-edit-dialog [name=project_name]').val(demoObject.name);
            if (typeof (demoObject.desc) !== undefined) {
                $('#demo-edit-dialog [name=project_desc]').val(demoObject.desc);
    $(function () {
        $('#demo-edit-dialog').on('click', '.modal-submit', function (e) {
            var form = $('#demo-edit-form');
            var url = form.attr('action');
            var data = form.serialize();
            var location = window.location;
                type: 'post',
                url: url,
                data: data,
                success: function (data) {
                    showMessageDlg('Ok', 'Action is done successfully!', { refreshURL: location });
                error: function (data) {
                    showMessageDlg('Error', 'Failed: ' + decodeURI(data.statusText));
Live Demo »