问题描述
DIV,非FORM,通过URL参数,这是第一页,所以city和pageid有默认值<div> <input id="city" type="hidden" value="zhengzhou"/> <input id="pageid" type="hidden" value="1"/> <input id="key" type="text" onkeypress="isearch()"/> <input type="button" id="btn" title="搜索" onclick="isearch()"/></div>function isearch(){var s="/search/";var key=encodeURI(encodeURI($("#key").val()));var city=$("#city").val();var page=$("#pageid").val(); //alert(s+"s?k="+key+"&city="+city+"&page="+page); top.location.href=s+"s?k="+key+"&city="+city+"&page="+page; //执行到这,地址栏中不是上面的正确的URL,不转向}回车可以弹出正确的网址:/search/s?k=keyword&city=bj&page=1但是地址栏中不是上面的正确的URL,还是当前页的URL如果点击button按钮,一切正常
解决方案
用window.location="xxxxxxxx";试试看。另外,按键以后要判断一下是不是回车。<!doctype html><html> <head> <title>Enter Goto URL</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function isearch() { var s="/search/"; var key=encodeURI(encodeURI($("#key").val())); var city=$("#city").val(); var page=$("#pageid").val(); window.location=s+"s?k="+key+"&city="+city+"&page="+page; } $(function() { $("#key").keyup(function(event){ if(event.keyCode == 13){ isearch(); } }); }); </script> </head> <body> <div> <input id="city" type="hidden" value="zhengzhou"/> <input id="pageid" type="hidden" value="1"/> <input id="key" type="text"/> <input type="button" id="btn" title="搜索" onclick="isearch()"/> </div> </body></html>