2010-09-17 62 views
0

我想使用一個「隱藏」的div,它將包含一個基於PHP返回值的可點擊圖像鏈接。我可以使用什麼來顯示/隱藏可點擊的按鈕?

有人可以請我指出一個例子嗎?我知道這有一個名字,但我不知道它叫什麼。

+1

我完全不知道你這是什麼意思。該div和它的可點擊內容應該隱藏嗎?請更具體一些。 – 2010-09-17 07:22:44

+0

Litso,我以爲我是特定的。是的,該div以及鏈接的按鈕應該隱藏,直到它得到PHP – John 2010-09-17 07:23:53

+0

的迴應。在你的一些評論中,你建議使用jquery進行報廢並用PHP解決問題。 jquery是一個客戶端腳本,隱藏/顯示將發生在客戶端系統上。 PHP是服務器端腳本,需要返回服務器以隱藏/顯示您的按鈕。在這種情況下,返回到服務器的行程可能是確定的,因爲hide/show是PHP調用的結果。你需要決定你想要隱藏/顯示某些東西的決定。 – 2010-09-18 20:37:04

回答

1

顯示:(api url

$("#mybuttondivID").show(); 

隱藏:(api url

$("#mybuttondivID").hide(); 

一個例子:

<html> 
<head> 
    <title> Show/Hide example</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
    <div id="mybuttondiv"> 
     <a href="http://www.google.com"><img src="http://www.google.be/intl/en_com/images/srpr/logo1w.png" alt="Google" /></a> 
    </div> 

    <script type="text/javascript"> 
     $(document).ready(function() { 

      // hide the buttondiv before we do the ajax call 
      $("#mybuttondiv").hide(); 

      //do an ajax request to a php page 
      $.ajax({ 
       type: "GET", 
       url: "fetch_data.php", 
       success: function (html) { 
        //do something when the response returns 
        // in this case, we make the button visible again 
        $("#mybuttondiv").show(); 
       }, 
       error: function (request) { 
        //ajax failed, display button again 
        $("#mybuttondiv").show(); 
       } 
      }); 
     }); 
    </script> 
</body> 
</html> 

這裏是jQuery的$.Ajax幫助頁面的鏈接: http://api.jquery.com/jQuery.ajax/

+0

嗨,彼得,謝謝你的回答。我完全是jquery文盲,所以有一個例子,你可以指向我,所以我可以看到代碼? – John 2010-09-17 07:29:35

+0

以我想要的「我認爲」的例子更新了我的答案。 – Peter 2010-09-17 07:50:32

0

也許你正在尋找的.hide().show().toggle()和/或$.get()組合?

編輯:基於以上的評論,也許這就是你追求的:

var hidden = $('#someDiv, #someLink').hide(); 
$.get('someScript.php', function() { 
    hidden.show(); 
}); 
+0

嗨凱文,謝謝你的答案。我真的不知道我需要什麼。你張貼的可能是票,但我真的jquery文盲。我即將準備廢除整個jquery庫,並直接在php中執行此操作。 – John 2010-09-17 07:27:58

+0

你究竟得到了什麼結果有點不清楚。如果這是PHP可以簡單處理的東西,我肯定會推薦它,而不是加載整個jQuery庫。 – Kevin 2010-09-17 07:32:40

+0

再次感謝凱文。在那裏不應該有成功嗎?如果服務器返回成功,我想顯示可點擊的圖片鏈接。 – John 2010-09-17 07:33:43

相關問題