Javascript 从url定义jQuery变量
我有一个jQuery搜索脚本,它使用选项卡让用户定义他们想要使用的搜索类型。当用户搜索时,会创建一个类似于#类型/查询/的URL。我需要在Javascript 从url定义jQuery变量,javascript,jquery,html,Javascript,Jquery,Html,我有一个jQuery搜索脚本,它使用选项卡让用户定义他们想要使用的搜索类型。当用户搜索时,会创建一个类似于#类型/查询/的URL。我需要在窗口.location.hash.replace中以某种方式定义查询词,这样我就只剩下URL中的搜索类型了。如何将下面示例中的querygoesher更改为用户所做的查询?我希望大家能理解我的问题 我的jQuery脚本是: $(document).ready(function () { $('[id^=type_]').click(function (
窗口.location.hash.replace
中以某种方式定义查询词,这样我就只剩下URL中的搜索类型了。如何将下面示例中的querygoesher更改为用户所做的查询?我希望大家能理解我的问题
我的jQuery脚本是:
$(document).ready(function () {
$('[id^=type_]').click(function () {
type = this.id.replace('type_', '');
$('[id^=type_]').removeClass('selected');
$('#type_' + type).addClass('selected');
return false;
});
if (window.location.hash != "") {
url = window.location.hash.replace('#', '').replace('/QUERYGOESHERE/', '');
$('#type_' + url).click();
} else {
$('#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;
});
if(window.location.hash!=“”){
url=window.location.hash.replace('#','').replace('/querygoesher/','');
$('#键入'+url.)。单击();
}否则{
$(“#键入_搜索”)。单击();
}
$('#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这样做就可以了
var full = window.location.hash.replace('#', '')
var queryType = full.substring(0,full.indexOf("/"))
var query = full.replace(queryType, '');
编辑:更新代码
if (window.location.hash != "") {
var full = window.location.hash.replace('#', '');
var queryType = full.substring(0,full.indexOf("/"));
console.log('#type_' + queryType);
$('#type_' + queryType).click();
}
一种可能是尾随/。我会对它做一些测试。@Callumwhite让我们看看这个部分应该有什么url?$(“#键入“+url”)。单击();