[英]how to do page redirection with two drop down menus selections with javascript
我是JavaScript的初學者,非常感謝您對我的問題的幫助。
我需要有兩個下拉菜單,每個菜單都帶有URL值,當選中它們時,將它們附加在一起並在單擊“提交”按鈕后重定向到選定的URL。
它類似於類似文章中的實時示例: http : //www.jsfiddle.net/Yxw7k/13
如果第一個下拉菜單中第二個選項的值是/a/something1
,而第二個下拉菜單中第三個選項的值是/3/index.html
,則在點擊/3/index.html
之后,您將被重定向到我希望能在不使用jQuery的情況下實現這一目標www.somesite.com/a/something1/3/index.html
我如何使用常規JS來實現這一目標?
我建議類似以下內容:
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');
}
JS小提琴演示 。
上面的代碼與以下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>
使用jquery更容易選擇值,您可以讓一個函數檢測用戶選擇的值,然后在用戶按下Submit按鈕時更新重定向鏈接。 因此,您需要將該函數添加到按鈕的onclick事件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.