Javascript / jQuery – 根据下拉选择转到URL
我有3个下拉框和一个go按钮。 我需要转到一个基于3个URL框中选择的内容构建的URL – 这是我的代码示例。
因此,例如,如果用户选择http:// + yahoo + .net – 然后点击“开始”按钮,他们将被发送到http://yahoo.net或者如果用户选择https // + hotmail + .com然后他们被发送到https://hotmail.com
是否有一些jQuery或Javascript代码可以检测下拉列表中的选择,然后构建正确的URL并在按下“Go”按钮时转到它?
谢谢扎克
var d1 = $("#dd1").find(":selected").attr("value"); var d2 = $("#dd2").find(":selected").attr("value"); var d3 = $("#dd3").find(":selected").attr("value"); location.href = d1+d2+d3+"";
像这样的东西?
window.location.href = $('#Dropwdown_1').val()+$('#Dropwdown_2').val()+$('#Dropwdown_3').val();
获取下拉列表的值
var searcher = document.getElementById("ddlSearch"); var searchDomain =searcher.options[searcher.selectedIndex].text;
其他两个相同
然后使用+连接字符串
var url = searchProtocol + searchDomain + searchTopLevel;
转到页面:
location.href= url;
@zach
这应该很简单。
- 使用选择HTML标记并为3个下拉菜单分配选项
- 在JS中创建一个函数createURL()。
- 获取三个框的值。 您可以使用document.getElementById(’Select1’)。options [document.getElementById(’Select1’)。selectedIndex] .value并使用加号连接。
- 您也可以使用JQuery来简化工作。
- 您可以使用window.location.href在同一页面中打开。
虽然其他答案有效……我宁愿以这种方式处理它(使用jQuery):
$("form").on("submit", function(e) { // Define our global url array var url = []; // Prevent the form from processing e.preventDefault(); // Loop through the drop down fields, storing the value of each into our "url" array $(this).find(".dropdown").each(function() { url.push($(this).val()); }); // Change the page location window.location = url.join(""); });