Javascript 检索散列值的AJAX JQUERY
下面是我使用ajax动态更改图像而不刷新页面的代码 HTMLJavascript 检索散列值的AJAX JQUERY,javascript,jquery,ajax,hash,Javascript,Jquery,Ajax,Hash,下面是我使用ajax动态更改图像而不刷新页面的代码 HTML <img id="design" alt="" width="300" height="300" /> <input type="button" id="GetImage" value="Get Image" /> php文件testimagelook.php连接到数据库,并返回我的一个图像的随机id。这段代码可以很好地显示图像,并将图像的id保存在URL的哈希中,这样我就可以保存图像的用户历史记录。但是,
<img id="design" alt="" width="300" height="300" />
<input type="button" id="GetImage" value="Get Image" />
php文件testimagelook.php连接到数据库,并返回我的一个图像的随机id。这段代码可以很好地显示图像,并将图像的id保存在URL的哈希中,这样我就可以保存图像的用户历史记录。但是,当用户单击“后退”按钮时,我不确定如何检索以前的哈希值并重新加载具有正确id的图像。有什么想法吗?试试这个:
$(document).ready(function(){
$("#GetImage").click(function() {
$.ajax({ //Make the Ajax Request
type: "POST",
url: "testimagelook.php", //file name
success: function(server_response){
var id = server_response;
document.location.hash = id;
$('#design').attr('src','img/boxes/'+id+'.png');
}
});
});
});
这很有魅力,谢谢你的帮助。(代码缺少“}”);(从末尾开始)另一个快速问题。是否有任何方法向他们提供指向此页面的链接,包括显示图像的哈希值,例如“mysite.com/page.php#12”,如果他们复制并粘贴到浏览器中,将显示id为12的图像?
$(document).ready(function(){
if (document.location.hash) {
updateImage();
}
$("#GetImage").click(function() {
$.ajax({ //Make the Ajax Request
type: "POST",
url: "testimagelook.php", //file name
success: function(server_response){
var id = server_response;
document.location.hash = id;
}
});
});
$(window).bind('hashchange',function(){
updateImage();
});
function updateImage() {
var id = document.location.hash.substring(1); // remove #
$('#design').attr('src','img/boxes/'+id+'.png');
}
});