1

这是非常一致的,但萤火虫显示我的 saveForm 函数没有从我的 'button.save' 事件处理程序中定义,但它适用于我的 'button.deleteForm' 事件处理程序:

    function saveForm(form)
    {

        var $form = form;
        var url = $form.attr('action');

        $.ajax({
               type: "POST",
               enctype: 'mutipart/form-data',
               url: url,
               data: $form.serialize(), // serializes the form's elements.
               success: function(data)
               {
                    // data is the server response.
                    // change this function to tell the
                    // user whether their submission 
                    // is correct or what fields have
                    // bad data.
                    var response = JSON.parse(data);
                    return true;
               }
             });
        return false; // avoid to execute the actual submit of the form.
    }

    // Do not use event handlers like .click(). This is the
    // only viable solution for handling events on dynamically
    // generated HTML elements. This handles the saving of data
    // to the server.
    $(document).on('click', 'button.save', function(e){
        var $form = $(this).closest('form'); 
        saveForm(form);            
    });

    // This event handler is responsible for deleting data.
    // For Joey's job: Please make sure that this calls save
    // after the user hits delete. This will save the data in
    // the database.
    $(document).on('click', 'button.deleteForm', function(e){

        // Get the form to update before deleting our embedded form
        var $form = $(this).closest('form');
        var str = $(this).attr('id');

        // Get the table id in review to delete
        var deleteForm = str + '_review';
        $('table#' + deleteForm).remove();

        // Get the collection form id to delete
        var idArray = str.split('_');
        idArray.pop();
        divId = '#' + idArray.join('_');
        $(divId).remove();

        saveForm($form);
    });
4

1 回答 1

1

你错过$了存档

$(document).on('click', 'button.save', function(e){
    var $form = $(this).closest('form'); 
    saveForm($form);  
     //------^----here          
});
于 2013-09-03T16:43:53.613 回答