0

I have a form inside a div tag, but with action="", and i want the result of form submission abppear in the same div, so i created this function:

$(document).ready(function() {
         $('#div1').delegate('form', 'submit', function() { // catch the form's submit events inside the div
        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('method'), // GET or POST
            url: $(this).attr('action'), // the file to call
            success: function(response) { // on success..
                 //$('#div1').html(response); // update the DIV
                $('#div1').fadeOut('slow',function() {
                            $('#div1').html(response).fadeIn('slow');
                    });
        }
        });
        return false; // cancel original event to prevent form submitting
    });
                });

however when i submit the form, nothing appears because the form has no action to be loaded, the same function works with other forms having action attribute, so my question is how to handle this case to make the form read the script in the same page when the form action is empty?

4

1 回答 1

2

首先,所有表格都应该有一个动作。它的无效 HTML 不是。

但是,如果我理解您想要发布到当前页面的问题,那么您可以这样做

    url: $(this).attr('action') || window.location.href, // the file to call

如果可用,这将采用 action 属性,如果没有操作,则采用当前窗口位置的 href。

于 2012-05-13T21:17:24.997 回答