2016-12-27 241 views
0

我正在做一個簡單的事情,但我不明白爲什麼會發生以下問題:當我在窗體中輸入一個值並單擊按鈕執行該功能時「加載」元素消失,無法顯示。按鈕點擊刪除dom隱藏元素阻止我顯示

的JavaScript如下:

<div> 
    <input id="magLog" name="magLog" type="text" style="width: 500px;"/> 
    <button id="StartITSMcheck">Check</button> 
</div> 
<div> 
    <div id="logContainer" class="filtersUNoBorder" style="padding-left: 8px; height:auto"> 
     <div id="loading" class="clear loading"> 
       Caricamento in corso...</div> 
     <!--Add text after check--> 
    </div> 
</div>  
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#loading").hide(); 
    }); 
    $("#StartITSMcheck").click(function() { 
     if ($("#magLog").val() != "" && $("#magLog").val() != undefined) { 
      $('#loading').show(); // show the loading message. 
      $.ajax({ 
       type: "GET", 
       url: '<%= Url.Action("StartITSMcheck", "Integration") %>', 
       async: false, 
       data: { "magLog": $("#magLog").val() }, 
       datatype: "html", 
       success: function (result) { 
        $("#logContainer").html(result); 
        $('#loading').hide(); // hide the loading message 
       }, 
       error: function (req, status, error) { 
        if (req.responseText.indexOf('12017') != -1) { 
         alert("CATCH: Status: " + status + " Error " + req.responseText); 
        } 
        else { 
         alert("Error getting data.com -> Req.responseText: " + req.responseText + "Req.status: " + req.status + " status: " + status + " error: " + error); 
        } 
       } 
      }); 
     } else $("#logContainer").html("<p>Gentilmente inserire un magazzino logico</p>"); 
    }); 
</script> 
+0

可能是您的函數正在進入錯誤回調。隱藏加載錯誤回調以及。 – Deep

+0

進行同步呼叫是一個壞主意。這也可能是加載圖形不顯示的原因。 – epascarello

+0

@Deep即使出現錯誤,我也必須隱藏它,但它不是顯示問題的固有內容。 – FeroX

回答

1
$("#logContainer").html(result); 

這可能是問題的原因。你用你的響應來替換div中的所有內容,包括你的加載元素。嘗試將它放在div外,如下所示:

<div id="logContainer" class="filtersUNoBorder" style="padding-left: 8px; height:auto"> 
    <!--Add text after check--> 
</div> 
<div id="loading" class="clear loading">Caricamento in corso...</div> 
+0

我排除了這種可能性,因爲點擊後立即消失了dom元素。我也嘗試了你的建議,事實上dom元素並沒有消失,但問題仍然存在:當我按下按鈕時,加載動畫在請求的未決狀態期間不顯示。 – FeroX

+0

嘗試在AJAX調用中使用'beforeSend'選項顯示加載元素。如果我使用調試,我會看到這些步驟是正確執行的。例如:'$ .ajax({發送:function(){$('#loading')。show()} }' –

+0

;但是如果我刪除斷點並按下按鈕,我不會銷售加載,但只有保持「點擊」幾秒鐘的按鍵。 – FeroX