2013-03-26 27 views
0

每次用戶單擊「Get Quote」按鈕時,都會顯示GIF動畫並隱藏舊顯示。JQuery只在執行前如果在它之前放置了Alert()

我查詢了一個後端服務,它返回了一些我剛剛轉儲到頁面上的HTML(我利用API中的SMARTY來處理我的糟糕的前端技能的格式)。當發生錯誤時,而不是HTML,JSON被返回(因此有點向後嘗試捕獲,在這種情況下,「成功」發生在「catch」中,而「失敗」發生在「try」中)。

發生錯誤時,HandleMessages()獲取響應,並處理存儲在那裏的消息(這很好地工作)。 處理完錯誤之後,GIF應該消失(因爲它在.collapse中,我隱藏它)。這不會發生,只有當我向頁面發出警告()時纔會發生。如果alert()被註釋或丟失,這不起作用。

我確定這是超級愚蠢或愚蠢的東西,但你的幫助總是很感激。

下面的代碼:

$('#findCarriers').click(function(){ 

    var data = new Object(); 

    $('#carrierLoad').collapse('show'); 
    $('#carriers').collapse('hide'); 

    data.general = { 
     'code': $('#warehouse').val(), 
     'shipper': $('#shipper_zip').val(), 
     'consignee': $('#consignee_zip').val(), 
     'shipment_type': $('#direction').val(), 
     'total_weight': $('#total_weight').text(), 
    }; 

    data.accessorials = []; 

    $('#accessorials').find('input:checkbox:checked').each(function(acc_index){ 

     data.accessorials.push($(this).val()); 

    }); 

    data.units = [{ 
     'num_of': $('#total_pallets').val(), 
     'type': 'pallet', 
     'weight': 0 
    }]; 

    data.products = []; 

    $('#items tr').each(function(index){ 

     data.products.push({ 
      'pieces': 1, 
      'weight': parseFloat($(this).find('.weight').val(), 10), 
      'class': parseInt($(this).find('.class').val(), 10) 
     }); 

    }); 

    $.post('index.php?p=api&r=html&c=ctsi&m=lcc', data, function(resp){ 
     try { 

      resp = $.parseJSON(resp); 
      handleMessages(resp); 

      carriersClear('find carriers'); 
      alert('good'); // comment out or remove and the show and hide on the collapse never happens 
      $('#carrierLoad').collapse('hide'); 
      $('#carriers').collapse('show'); 
     } catch(err) { 

      $('#carriers').html(resp); 
      alert('fail'); // comment out or remove and the show and hide on the collapse never happens 
      $('#carrierLoad').collapse('hide'); 
      $('#carriers').collapse('show'); 

     } 

    }); 

    return false; 

}); 

輔助功能:

function setError(e_title, e_msg, e_type) { 

    $.pnotify({ 
     title: e_title, 
     text: e_msg, 
     type: e_type 
    }); 

} 

function handleMessages(jsResponse) { 

    $.each(jsResponse.message, function(){ 

     setError(this.traceroute[0] + '-' + this.traceroute[1] + '-' + this.traceroute[2], this['message'], this['type']) 

    }); 

} 

function carriersClear(reason) { 

    $('#carriers').html(''); 

    //alert(reason); 

} 

這裏是HTML:

<div class="row"> 

<div class="span10"> 

    <h3 class="heading" data-toggle="collapse" data-target="#carrierSelect">Carriers</h3> 

    <div class="bg-light collapse in bg-light" id="carrierSelect"> 

     <div class="row spacer-top spacer-bottom"> 

      <div class="span2 offset8"> 
       <a href="#" class="btn btn-primary" id="findCarriers">Get Quote</a> 
      </div> 

     </div> 

     <div class="row spacer-bottom collapse" id="carrierLoad"> 
      <div class="span2 offset4"><img class="center-block" src="view/img/load.gif" /></div> 
     </div> 

     <div class="row"> 

      <div class="span10 bg-x-light collapse in spacer-top" id="carriers"> 

      </div> 

     </div> 

    </div> 

</div> 

+4

如果某樣東西時用一個'警報()',那就是你有一些異步行動正在進行一個明確的信號,並且該警示使得延遲不所有的區別。 – adeneo 2013-03-26 17:09:42

+0

'handleMessages()'和'carriersClear()'的代碼在哪裏? – JJJ 2013-03-26 17:11:09

+0

你能否詳細說明。我對jQuery基本沒用,所以說明會很棒,建議的修復也會很棒。 – DavidScherer 2013-03-26 17:11:26

回答

0

你需要傳遞carriersClear後的行()進入th e運營商清算功能作爲回調。像這樣:

$.post('index.php?p=api&r=html&c=ctsi&m=lcc', data, function(resp){ 
    try { 

     resp = $.parseJSON(resp); 
     handleMessages(resp); 

     carriersClear('find carriers', function(){ 
      $('#carrierLoad').collapse('hide'); 
      $('#carriers').collapse('show'); 
     } 
    } 

則:

function carriersClear(reason,callback) { 
    $('#carriers').html(''); 
    callback(); 
} 
+0

感謝伴侶,作品像一個魅力。 :) – DavidScherer 2013-03-26 18:59:43

+0

經過更多的修補之後,我實際上將它連接在引導程序崩潰事件的周圍,但你讓我在我的路上設置。 – DavidScherer 2013-03-28 02:18:38

相關問題