2014-07-04 41 views
0

我有一个表格和一段javascript代码来创建AJAX表单。奇怪的是,当我在Internet Explorer中提交表单时,唯一显示的是[object Object]。该表单在Google Chrome中运行良好。这里是我的代码:无法取消对Ajax表单的默认操作提交

表头:

<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

后有没有人有一个想法?如果我需要发布更多的代码,请让我知道。如果你愿意,我可以发布所有的代码。

非常感谢!

+0

'e.returnValue = false ;;'double semicons???为什么不试试'e.preventDefault();'?为什么你需要这样的条件? – Jai

+0

查看更新...... –

+0

对不起,我刚刚贴错了代码。我更新它知道完整的JavaScript代码。 –

回答

1

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

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

stil doens'工作。作为第一条规则,我添加了'return false'结尾和'e.preventDefault();'。但是现在产量不一样。 Internet Explorer现在打印JSON响应而不是'[object Object]'。所以它只是提交表单,因为它转到SITE/pages/save/1。 –

+0

请提供什么不起作用的确切说明。 –

+0

它看起来像'e.preventDefault? e.preventDefault():e.returnValue = false;''可以防止默认,因为它不会重定向到SITE/save/details/1,而是显示'[object Object]'。所以当我提交表单时,Internet Explorer会显示'[object Object]',但我只需在AJAX调用之后执行AJAX调用和操作。具体问题是为什么Internet Explorer显示此消息而不是对当前窗口无所作为? –