document.location.href 未更新共享点中的 onkeypress

发布于 2024-11-03 19:16:55 字数 849 浏览 4 评论 0原文

我在通过回车键访问时执行页面重定向功能时遇到问题。基本上,onkeypress=Enter 或单击“搜索”时,页面应重定向到预设 URL 并将搜索字符串附加到查询中。

如果我手动单击“搜索”,重定向会起作用,但是,如果我只是按回车键,则重定向不会起作用。我添加了一个警报以确保搜索功能正在触发,但 document.location.href 没有重定向页面。在 FF4 中,它刷新页面(但保留搜索字符串)。在 IE7 中,它会关闭窗口。

[编辑] 我在 Sharepoint 网站上使用它似乎是相关的。该代码在 Sharepoint 之外运行良好。 [/编辑]

下面的示例简化了我所实现的内容,但重现了问题。

<script type="text/javascript">
function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>&nbsp;
<a id="SearchButton" href="javascript:mySearch();" />Search</a>

有人可以帮忙吗?

I have an issue with a page redirect function executing when accessed by the enter key. Basically, onkeypress=Enter or when Search is clicked the page should redirect to a preset url and append a searchstring to the query.

The redirect works if I manually click 'Search', however, if I just hit enter it does not. I added an alert to make sure the search function is firing, which it is, but the document.location.href is not redirecting the page. In FF4, it refreshes the page (but preserves the searchstring). In IE7, it closes the window.

[edit] It seems to be pertinent that I'm using this on a Sharepoint site. The code works fine outside of Sharepoint. [/edit]

The example below simplifies what I have implemented, but recreates the problem.

<script type="text/javascript">
function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/> 
<a id="SearchButton" href="javascript:mySearch();" />Search</a>

Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

墨落画卷 2024-11-10 19:16:55

尝试:

window.location = url; // instead of: document.location.href = url;

您的注册代码似乎在这里工作: http://jsfiddle.net/maniator/RzhXy/

try:

window.location = url; // instead of: document.location.href = url;

and you reg code seems to be working here: http://jsfiddle.net/maniator/RzhXy/

感情废物 2024-11-10 19:16:55

您可以将: 替换

document.location.href = url;

为:

window.document.aspnetForm.action = url;

这在 SharePoint 2010 网站中对我有用

You can replace:

document.location.href = url;

with:

window.document.aspnetForm.action = url;

This has worked for me in SharePoint 2010 websites

成熟稳重的好男人 2024-11-10 19:16:55

我在 XPages (IBM) 上也遇到了同样的问题。我认为这些框架有一些共同的问题。我所做的是在触发 window.location.href 之前对事件使用 PreventDefault() 函数。

if(thisEvent.keyCode == 13){
    thisEvent.preventDefault();
    window.location.href = "newpage.html";
}

我无法解释为什么它有效;我刚刚尝试了瞧!我希望它对将来的人有所帮助。 (现在我可以收回赏金了呵呵)

I had the same problem with XPages (IBM). I think these frameworks has some problem in common. What I did is to use the preventDefault() function on the event before triggering the window.location.href.

if(thisEvent.keyCode == 13){
    thisEvent.preventDefault();
    window.location.href = "newpage.html";
}

I can't explain why it works; I just tried an voila! I hope it helps someone in the future. (Now I can retract the bounty hehehe)

木槿暧夏七纪年 2024-11-10 19:16:55

摆脱.href。只需设置 set document.location 即可更改 URL。您当前的版本应该可以工作...

document.location = url;

Get rid of the .href. Just set set document.location to change the URL. Your current version should work though...

document.location = url;
如梦初醒的夏天 2024-11-10 19:16:55

确保您绝对没有其他操作来响应 onkeyup 事件,在我的例子中,window.onkeyup 正在调用一些其他元素的 onclick 处理程序,一切都变得一团糟。

Make sure you absolutely have no other action as a response to onkeyup event, in my case the window.onkeyup was calling some other elements onclick handler and everything got messed up.

黑色毁心梦 2024-11-10 19:16:55

只是试试这个

<script type="text/javascript">
    function mySearch()
    {
        if(event.keyCode==13)
        {
            var SearchString = document.getElementById("SearchBox").value;
            var url = "http://stackoverflow.com/search?q="+SearchString;
            alert(SearchString);
            document.location.href = url;
        }
    }
</script>
<input id="SearchBox" onkeypress="mySearch();"/> 
<a id="SearchButton" href="javascript:mySearch();" />Search</a>

顺便说一句,你的旧功能在我的所有浏览器上运行良好

just try this

<script type="text/javascript">
    function mySearch()
    {
        if(event.keyCode==13)
        {
            var SearchString = document.getElementById("SearchBox").value;
            var url = "http://stackoverflow.com/search?q="+SearchString;
            alert(SearchString);
            document.location.href = url;
        }
    }
</script>
<input id="SearchBox" onkeypress="mySearch();"/> 
<a id="SearchButton" href="javascript:mySearch();" />Search</a>

BTW you old function is working fine on my ALL Browser

雪化雨蝶 2024-11-10 19:16:55

手动点击有效吗?然后您就可以获得快速修复解决方案!
if (event.keyCode == 13) document.getElementById('SearchButton').click();

如果它不起作用,也可以尝试 .onclick()。

Manual click works? Then you can have a quick fix solution!
If (event.keyCode == 13) document.getElementById('SearchButton').click();

If it doesn't work, try .onclick() too.

臻嫒无言 2024-11-10 19:16:55

在 SharePoint 2010 CWEP 中测试了原始代码,它似乎可以在 IE7 中工作,
在 SharePoint 2007 CEWP 中进行了测试,它可以在 IE9 和 FF4

Stab 中正常工作,但您可以尝试删除具有错误返回值的单个函数调用的内联代码。我以前在 SharePoint 中遇到过类似的问题。

<script type="text/javascript">
function myKeypress(){
   if (event.keyCode == 13) 
     mySearch();
   return false;
}

function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="myKeypress();"/>
<a id="SearchButton" href="javascript:mySearch();">Search</a> 

Tested the original code in SharePoint 2010 CWEP and it appears to work in IE7,
tested in a SharePoint 2007 CEWP and it works in IE9 and FF4

Stab in the dark but you can try removing the inline code for a single function call that has a false return value. I have had problems like that before in SharePoint.

<script type="text/javascript">
function myKeypress(){
   if (event.keyCode == 13) 
     mySearch();
   return false;
}

function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="myKeypress();"/>
<a id="SearchButton" href="javascript:mySearch();">Search</a> 
深海不蓝 2024-11-10 19:16:55

添加 return false 以停止 asp.net 表单继续处理

<script type="text/javascript">
function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
    //add return false to stop asp.net form from continuing to process
    return false;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/> 
<a id="SearchButton" href="javascript:mySearch();" />Search</a>

add return false to stop asp.net form from continuing to process

<script type="text/javascript">
function mySearch() {
    var SearchString = document.getElementById("SearchBox").value;
    var url = "http://stackoverflow.com/search?q="+SearchString;
    alert(SearchString);
    document.location.href = url;
    //add return false to stop asp.net form from continuing to process
    return false;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/> 
<a id="SearchButton" href="javascript:mySearch();" />Search</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文