根据下拉列表中的选择转发到新页面,无需使用 JavaScript
我正在尝试从 JSP 中删除 JavaScript。目前我正在使用类似的东西根据下拉列表中的选择转发到我需要的页面。
function STFS() {
var url = document.stform.typeOptions.options[document.stform.typeOptions.selectedIndex].value;
if (url!=0) {
window.location=url;
}
}
我怎样才能在不使用 javascript 的情况下做同样的事情。 我尝试在“select onChange”中设置 window.location,如下所示:
<select name="typeOptions" onChange="window.location=this.options[this.selectedIndex].value;">
但是当我提交表单时,它不会转到预期的网址。我一定是错过了什么。 这是一个portlet环境,URL比较长。 任何帮助表示赞赏。谢谢
I am trying to remove JavaScript from my JSP. Currently i am using something like this to forward to the page i need based on the selection in the drop-down list.
function STFS() {
var url = document.stform.typeOptions.options[document.stform.typeOptions.selectedIndex].value;
if (url!=0) {
window.location=url;
}
}
How can i do the same thing without using javascript.
I tried setting the window.location in the 'select onChange' as follows:
<select name="typeOptions" onChange="window.location=this.options[this.selectedIndex].value;">
But when i submit the form, it doesn't go to the expected url. I must be missing something.
This is a portlet environment and the URL is relatively long.
Any help is appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用onchange
而不是onChange
(这不是真正的问题,因为很多浏览器对此很宽容,但这显然是不正确的)并确保您正在运行您认为您正在运行的代码(即您没有运行旧版本或浏览器缓存版本)并且您实际上正在更改下拉项,而不是简单地按表单提交按钮(onchange
当您提交时不会运行表单)。根据评论更新
将其更改如下:
使用
RedirectServlet
类,该类映射到/redirect
的 URL 模式,并且基本上在中执行以下工作doGet()
方法:Useonchange
instead ofonChange
(not the real problem since lot of browsers are forgiving on this, but it is plain incorrect) and ensure that you're running the code you think you're running (i.e. you're not running an old or browser-cached version) and that you're really changing the dropdown item instead of plain pressing the form submit button (theonchange
doesn't run when you submit the form).Update as per the comment
Change it as follows:
with a
RedirectServlet
class which is mapped on an URL pattern of/redirect
and does basically the following job indoGet()
method: