我有一个jQuery搜索脚本,它使用选项卡为用户定义他们想要的搜索类型。使用$('#type_search').click();
选择选项卡作为默认选项卡,但是这会导致刷新结果页面时出现问题。如果选择了不同的搜索类型,然后刷新页面,则自动选择默认选项卡,因此即使页面URL说它是正确的,它也不会返回正确的结果。
我的问题是,如果查询是活动的,如果没有活动查询使用默认选项卡,我如何从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);
}
});
});
if (window.location.hash.indexOf('#' + type + '/') == 0) {
query = window.location.hash.replace('#' + type + '/', '').replace('/', '');
$('#query').val(decodeURIComponent(query)).keyup();
}
var textlength = $('#query').val().length;
if (textlength <= 0) {
$('#query').focus();
} else {
$('#query').blur();
}
});
啊,我也有这个问题。这很简单。在页面准备好了,你只需从URL中获取锚并模拟点击。
$(document).ready(function() {
url = document.location.href.split('#');
$('#'+url[1]).click();
});