【发布时间】:2011-07-17 01:52:38
【问题描述】:
我有一个 jQuery 搜索脚本,它使用选项卡让用户定义他们想要使用的搜索类型。当用户搜索时,会创建一个类似于 #type/query/ 的 URL。但是,当您重新加载页面时,单击转到不同页面的结果或从前一页面返回时,搜索结果不再存在。为什么会这样,我该如何解决这个问题?我也不想用任何插件。
我的 jQuery 代码是:
$(document).ready(function () {
$('[id^=type_]').click(function () {
type = this.id.replace('type_', '');
$('[id^=type_]').removeClass('selected');
$('#type_' + type).addClass('selected');
return false;
});
$('#type_search').click();
$('#query').keyup(function () {
var query = $(this).val();
var url = '/' + type + '/' + query + '/';
window.location.hash = '' + type + '/' + query + '/';
document.title = $(this).val() + ' - My Search';
$('#results').show();
if (query == '') {
window.location.hash = '';
document.title = 'My Search';
$('#results').hide();
}
$.ajax({
type: 'GET',
url: url,
dataType: 'html',
success: function (results) {
$('#results').html(results);
}
});
});
var textlength = $('#query').val().length;
if (textlength <= 0) {
$('#query').focus();
} else {
$('#query').blur();
}
});
【问题讨论】:
标签: javascript jquery html