[英]how to do page redirection with two drop down menus selections with javascript
I am a total beginner to JavaScript and appreciate your help with my question. 我是JavaScript的初学者,非常感谢您对我的问题的帮助。
I need to have two drop down menus, each with URL values, when selected, would append together and redirect to the selected URL after hitting a submit button. 我需要有两个下拉菜单,每个菜单都带有URL值,当选中它们时,将它们附加在一起并在单击“提交”按钮后重定向到选定的URL。
It is similar to the live example from a similar article: http://www.jsfiddle.net/Yxw7k/13 它类似于类似文章中的实时示例: http : //www.jsfiddle.net/Yxw7k/13
If the value of the second option in the first drop down was lets say: /a/something1
, and the value of the third option in the second drop down was /3/index.html
, after hitting submit you'd be redirected to www.somesite.com/a/something1/3/index.html
I am hoping to achieve this without the use of jQuery. 如果第一个下拉菜单中第二个选项的值是
/a/something1
,而第二个下拉菜单中第三个选项的值是/3/index.html
,则在点击/3/index.html
之后,您将被重定向到我希望能在不使用jQuery的情况下实现这一目标www.somesite.com/a/something1/3/index.html
How could I use regular JS to achieve this? 我如何使用常规JS来实现这一目标?
I'd suggest something like the following: 我建议类似以下内容:
function makePath(directory, file, domain) {
if (!directory || !file) {
return false;
}
else {
directory = directory.nodeType == 1 ? directory.value : document.getElementById(directory).value;
file = file.nodeType == 1 ? file.value : document.getElementById(file).value;
return newURL = [domain || 'http://' + location.hostname, directory, file].join('/');
}
}
document.getElementById('fileName').onblur = function(){
// using console.log() in the demo
document.location = makePath('directoryPath', this, 'http://example.com');
}
The above is used with the following HTML: 上面的代码与以下HTML结合使用:
<select id="directoryPath">
<option>directoryA</option>
<option>directoryB</option>
<option>directoryC</option>
</select>
<select id="fileName">
<option>file1.html</option>
<option>file2.html</option>
<option>file3.html</option>
</select>
with jquery is more easy select the values, you could make a function who detect the values selected and then update the redirection link when the user press the submit button. 使用jquery更容易选择值,您可以让一个函数检测用户选择的值,然后在用户按下Submit按钮时更新重定向链接。 So you need add the function to the onclick event of the button.
因此,您需要将该函数添加到按钮的onclick事件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.