2014-07-04 39 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顯示此消息而不是對當前窗口無所作爲? –