Javascript 重新加载页面后启动jQuery搜索
我有一个jQuery搜索脚本,它使用选项卡让用户定义他们想要使用的搜索类型。当用户搜索时,会创建一个类似于#类型/查询/的URL。但是,当您重新加载页面、单击转到其他页面的结果或从上一页面返回时,搜索结果不再存在。我的问题是,当重新加载页面时,如何再次启动jQuery搜索 我的jQuery代码是:Javascript 重新加载页面后启动jQuery搜索,javascript,jquery,html,Javascript,Jquery,Html,我有一个jQuery搜索脚本,它使用选项卡让用户定义他们想要使用的搜索类型。当用户搜索时,会创建一个类似于#类型/查询/的URL。但是,当您重新加载页面、单击转到其他页面的结果或从上一页面返回时,搜索结果不再存在。我的问题是,当重新加载页面时,如何再次启动jQuery搜索 我的jQuery代码是: $(document).ready(function () { $('[id^=type_]').click(function () { type = this.id.repl
$(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 Script';
$('#results').show();
if (query == '') {
window.location.hash = '';
document.title = 'My Search Script';
$('#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();
}
});
$(文档).ready(函数(){
$('[id^=type_quo;])。单击(函数(){
type=this.id.replace('type_','');
$('[id^=type_quo;]).removeClass('selected');
$('#type'+type).addClass('selected');
返回false;
});
$(“#键入_搜索”)。单击();
$('#query').keyup(函数(){
var query=$(this.val();
var url='/'+type+'/'+query+'/';
window.location.hash=''+type+'/'+query+'/';
document.title=$(this.val()+'-我的搜索脚本';
$(“#结果”).show();
如果(查询=“”){
window.location.hash='';
document.title='我的搜索脚本';
$(“#结果”).hide();
}
$.ajax({
键入:“GET”,
url:url,
数据类型:“html”,
成功:功能(结果){
$('#results').html(results);
}
});
});
var textlength=$('#query').val().length;
如果(textlength既然散列代码应该保留在window.location.hash中,为什么不重新读取散列呢?这里有一个小教程:既然散列代码应该保留在window.location.hash中,为什么不重新读取散列呢?这里有一个小教程:你需要做的就是在查询时做同样的事情,但是在页面加载时单击“吨”:
$(function () {
if (window.location.hash != "") {
// run the search function here
}
});
您只需在页面加载时执行与单击查询按钮时相同的操作:
$(function () {
if (window.location.hash != "") {
// run the search function here
}
});
在domready上,检查url上是否存在哈希。如果存在,则以与此处回答相同的方式运行Search。在domready上,检查url上是否存在哈希。如果存在,则以与此处回答相同的方式运行Search。