不能取消ajax表单提交的默认操作


Can't cancel default action on ajax form submit

我有一个表单和一块javascript代码来创建AJAX表单。奇怪的是,当我在ie提交我的表单时,唯一显示的是[object Object]。该表单在谷歌浏览器中运行良好。下面是我的代码:

表单标题:

<form id="page-details-form" class="ajax-form" name="page-details-form" method="POST" action="/pages/save/1">

Javascript侦听器:

<script>
    $(function(){
        $(document).on("submit",".ajax-form",function(e){
            if (window.event) {
                window.event.returnValue = false;
            }           
            e.preventDefault ? e.preventDefault() : e.returnValue = false;
            $('.form-message').remove();
            if($('#onSubmitJsEval').html() && $('#onSubmitJsEval').html().length > 0)
            {
                eval($('#onSubmitJsEval').html());
            }
            var submitBtnValue  = $('#submitBtn').html();
            var formId          = $(this).attr('id');
            var postData        = $(this).serializeArray();
            $('#submitBtn')     .html('<img src="images/ajax-loader.gif" />');
            $('p.has-error')    .remove();
            $('div.has-error')  .removeClass('has-error');
            $.post($(this).attr('action'), postData, function(jsonResponse)
            {
                var jsonObject = jQuery.parseJSON(jsonResponse);
                if(jsonObject.success == true)
                {
                    $('<div class="<?=MESSAGE_SUCCESS_CLASS?>"><?=MESSAGE_SUCCESS_PREFIX?>'+jsonObject.message+'</div>' ).insertBefore( "#" + formId + " h2" );
                    if(jsonObject.insertedId > 0)
                    {
                        var stringPath = window.location.pathname.substr(window.location.pathname.length - 1);
                        document.location.href = window.location.pathname + ((stringPath != "/") ? "/" : "") + jsonObject.insertedId;
                    }
                }
                else
                {
                    $('<div class="<?=MESSAGE_ERROR_CLASS?>"><?=MESSAGE_ERROR_PREFIX?>'+jsonObject.message+'</div>' ).insertBefore( "#" + formId + " h2" );
                    $.each(jsonObject.errors, function(index, value){
                        $('[name='+index+']').parent().addClass('has-error');
                        $('[name='+index+']').after('<p class="has-error help-block">'+value+'</p>');
                    })
                }
                $('#submitBtn').html(submitBtnValue);
            });
        });

    }); 
</script>

除了当前选项之外,我尝试了几个选项:

  • if (e.preventDefault) e.preventDefault();
  • e.returnValue = false
  • e.returnValue = falsee.preventDefault

有谁知道吗?如果我需要发布更多的代码,请让我知道。如果你需要的话,我可以把所有的代码都贴出来。

多谢!

您将需要e.p preventdefault()和return false的组合,并在两者之间使用处理代码。

$(document).on("submit", ".ajax-form", function(e) {
    e.preventDefault();
    //Do your stuff here
    return false;
});