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;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?