document.location.href 未更新共享点中的 onkeypress
我在通过回车键访问时执行页面重定向功能时遇到问题。基本上,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();"/>
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试:
您的注册代码似乎在这里工作: http://jsfiddle.net/maniator/RzhXy/
try:
and you reg code seems to be working here: http://jsfiddle.net/maniator/RzhXy/
您可以将: 替换
为:
这在 SharePoint 2010 网站中对我有用
You can replace:
with:
This has worked for me in SharePoint 2010 websites
我在 XPages (IBM) 上也遇到了同样的问题。我认为这些框架有一些共同的问题。我所做的是在触发 window.location.href 之前对事件使用 PreventDefault() 函数。
我无法解释为什么它有效;我刚刚尝试了瞧!我希望它对将来的人有所帮助。 (现在我可以收回赏金了呵呵)
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.
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)
摆脱.href。只需设置 set document.location 即可更改 URL。您当前的版本应该可以工作...
Get rid of the .href. Just set set document.location to change the URL. Your current version should work though...
确保您绝对没有其他操作来响应 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.
只是试试这个
顺便说一句,你的旧功能在我的所有浏览器上运行良好
just try this
BTW you old function is working fine on my ALL Browser
手动点击有效吗?然后您就可以获得快速修复解决方案!
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.
在 SharePoint 2010 CWEP 中测试了原始代码,它似乎可以在 IE7 中工作,
在 SharePoint 2007 CEWP 中进行了测试,它可以在 IE9 和 FF4
Stab 中正常工作,但您可以尝试删除具有错误返回值的单个函数调用的内联代码。我以前在 SharePoint 中遇到过类似的问题。
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.
添加 return false 以停止 asp.net 表单继续处理
add return false to stop asp.net form from continuing to process