jQuery/JavaScript:将URL哈希从<;输入>;标记或在表单上提交

jQuery/JavaScript:将URL哈希从<;输入>;标记或在表单上提交,javascript,hash,input,Javascript,Hash,Input,是否可能以某种方式影响来自标记的URL哈希 我需要在不刷新页面的情况下启动搜索,但还想更改URL哈希,例如:search.html#query=hello%20world,当我键入hello world时,尤其是并点击Enter(或submit) UPD:我有一个AJAX搜索,但我想让历史记录的后退和前进按钮保持工作 因此,每次用户搜索某个内容时,我希望他停留在同一页面上,通过AJAX加载搜索结果,并更改页面的URL哈希以启用历史按钮。使用window.location.hash属性获取并设置哈

是否可能以某种方式影响来自
标记的URL哈希

我需要在不刷新页面的情况下启动搜索,但还想更改URL哈希,例如:
search.html#query=hello%20world
,当我键入
hello world
时,尤其是
并点击Enter(或submit)

UPD:我有一个AJAX搜索,但我想让历史记录的后退和前进按钮保持工作


因此,每次用户搜索某个内容时,我希望他停留在同一页面上,通过AJAX加载搜索结果,并更改页面的URL哈希以启用历史按钮。

使用
window.location.hash
属性获取并设置哈希

var e = document.getElementById('search');
window.location.hash = "#"+escape(e.value);


.

由于您是异步执行此操作的,因此我无法确切地告诉您如何执行此操作,但我假设您具有某种搜索功能,在该功能中,您可以获取
输入的值并执行搜索:

function your_search_function()
{
    var searchString = document.getElementById('your-input').val // get the value

    // do your search stuff...

    window.location.hash = 'query=' + encodeURIComponent(searchString);
}

使用AJAX搜索时,请注意提交时的
onsubmit
-根据您的HTML,事件可能永远不会触发。是的,我目前正在使用带有“return false”的提交处理程序,它还能工作吗?
function your_search_function()
{
    var searchString = document.getElementById('your-input').val // get the value

    // do your search stuff...

    window.location.hash = 'query=' + encodeURIComponent(searchString);
}