2012-02-08 49 views
0

如何在點擊同一頁面上的另一個元素時僅使用Jquery或Javascript刷新HTML頁面的元素?我需要知道這個實現表單驗證碼。如何使用jquery javascript重新加載部分表單?

<img src="includes/captcha.php" style="padding:10px 0;"/>&nbsp;&nbsp;&nbsp; 
<img src="images/refresh.gif" onclick="window.location.reload()" style="cursor:pointer; padding:10px 0;" /> 

我想通過點擊refresh.gif重裝王氏captcha.php產生的圖像。當前的代碼重新加載完整的頁面。

回答

2

這不需要任何的jQuery。只需更改圖像src屬性以包含一些附加的隨機數據。

function reloadImage() { 
    var now = new Date(); 
    document.getElementById('captcha').src = 'includes/captcha.php?r=' + now.getTime(); 
} 

改變你的圖片標籤來設置這樣的ID。

<img src="includes/captcha.php" id="captcha" style="padding:10px 0;"/> 

然後在刷新圖像中添加對函數的調用。

<img src="images/refresh.gif" onclick="reloadImage()" style="cursor:pointer; padding:10px 0;" /> 
+0

我應該如何獲取並處理captcha.php中的其他數據?它不工作! – 2012-02-08 13:06:19

+0

不,重點僅僅是改變URL,以便瀏覽器重新加載圖像。額外的'r'參數根本不應該做任何事情。哎呀,我傻了,忘了一個新的約會。現在編輯答案 – Dervall 2012-02-08 13:08:44

+0

我做了同樣的..但仍然不工作.. – 2012-02-08 13:09:37

1

你需要使用什麼是jQuery函數的load()

舉例來說,如果你想用ID = A div的內容來替代使用id =「myDiv」一個div的內容「theirDiv」從遠程頁面的HTML,請執行此操作:

$('#myDiv').load('http:// theirurl #theirDiv');

http://api.jquery.com/load

+0

謝謝您的回答! – 2012-02-08 13:25:00