doutanggun9816 2014-07-04 06:40
浏览 51

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

I have a form and a piece of javascript code to create AJAX forms. The weird thing is when i submit my form in Internet Explorer the only thing that displays is [object Object]. The form works fine in Google Chrome. Here is my code:

Form header:

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

Javascript listener:

<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>

I tried several options besides the current option:

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

Does anyone have a idea? If i need to publish more code please let me know. I can post all the code if you want.

Many thanks!

  • 写回答

1条回答 默认 最新

  • douqu8828 2014-07-04 07:30
    关注

    You will need a combination of e.preventDefault() and return false, with handling code inbetween.

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

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单