在div中没有​​动作的加载表单

时间:2012-05-13 21:13:53

标签: javascript ajax jquery

我在div标签中有一个表单,但是action="",我希望表单提交的结果abppear在同一个div中,所以我创建了这个函数:

$(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
    });
                });

然而,当我提交表单时,没有出现任何内容,因为表单没有要加载的操作,相同的功能适用于具有action属性的其他表单,所以我的问题是如何处理这种情况使表单读取脚本表单操作为空时的同一页面?

1 个答案:

答案 0 :(得分:2)

首先,所有表格应始终有一个动作。它的HTML无效。

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

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

这将获取action属性(如果可用),或者当前窗口位置的href(如果没有操作)。

相关问题