如何使用jquery javascript重新加载表单的一部分?

时间:2012-02-08 12:43:16

标签: jquery refresh

如何在单击同一页面上的另一个元素时仅使用Jquery或Javascript刷新HTML页面的元素?我需要知道这个以实现表单验证码。

<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 个答案:

答案 0 :(得分: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;" />

答案 1 :(得分:1)

您需要使用的是jQuery函数load()

例如,如果要将id ='myDiv'的div的内容替换为远程页面的HTML中id ='theirDiv'的div的内容,请执行以下操作:

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

http://api.jquery.com/load