无法取消对ajax表单提交的默认操作

时间:2014-07-04 06:40:13

标签: php jquery ajax forms internet-explorer

我有一个表单和一段javascript代码来创建AJAX表单。奇怪的是,当我在Internet Explorer中提交表单时,唯一显示的是[object Object]。该表单在Google Chrome中运行良好。这是我的代码:

表格标题:

<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 = false 之后
  • e.preventDefault

有没有人有想法?如果我需要发布更多代码,请告诉我。如果你愿意,我可以发布所有代码。

非常感谢!

1 个答案:

答案 0 :(得分:1)

您将需要e.preventDefault()的组合并返回false,并在其间处理代码。

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