2015-05-14 24 views
0

任何人都可以告訴我如何我可以隱藏/刪除加載按鈕,當我收到來自ajax調用(例如我有未定義或空值)的某些響應?我嘗試使用$('#mango')。hide();但它並沒有爲我刪除按鈕!但是我收到了警報,確認我從api獲得了null或undefined值。希望有人能幫我解決這個.Thanks如何隱藏ajax響應中的onclick按鈕?

代碼:

<script> 
var maxnumId = null; 
function callApi() { 
    $.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     cache: false, 
     url: "https://api.somesite.com/......"), 
     success: function(data) { 
     maxnumId = data.pagination.next_num_id; 

      if (maxnumId === undefined || maxnumId === null) { 

       alert('End!'); 
       //remove the loadmore button here 
        $('#mango').hide(); 
       } 
     } 

    }); 
} 
</script> 
<body> 
<br> 
<center> 

<button id="mango" onclick="callApi()">Load More</button> 

</html> 
+1

什麼是'maxnumId'值?它是否在控制檯中表示「undefined」或「null」? –

+0

.hide()隱藏元素。 .remove()將其從DOM中刪除。 –

+0

你試過刪除?你能寫你的html代碼嗎? – nole

回答

2

假設,如果條件滿足(maxnumId === undefined || maxnumId === null),你可以嘗試做純JS:

document.getElementById('mango').style.visibility = 'hidden'; 
document.getElementById('mango').style.display = 'none'; 
+0

非常感謝所有的答覆。萊安德羅加布裏埃爾卡薩斯你的解決方案工作! – user1788736