[英]Selecting Value from drop downs to go to URL
I'm trying to setup a drop down menu, and have the final selection bring you to a URL. 我正在尝试设置下拉菜单,并让最终选择带您到URL。 I can get the various drop downs working, but the browser doesnt do anything after you make your final selection.
我可以使各种下拉工作,但浏览器在您做出最终选择后没有做任何事情。
I can get close, but then I lose the ability to hid the other drop downs that don't apply! 我可以靠近,但后来我失去了隐藏不适用的其他下降的能力!
<tr> <td> <p align="center"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for="qmt-vehicle">Vehicle:</label> <select id="qmt-vehicle" name="vehicle"> <option></option> <option class="BMW" value="BMW">BMW</option> <option class="Audi" value="Audi">Audi</option> </select> <p align="center"> <label for="qmt-manufacturer">Manufacturer:</label> <select id="qmt-manufacturer" name="manufacturer"> <option></option> <option class="BMW" value="http://www.google.com">BMW</option> <option class="BMW" value="http://www.google.com"> M3</option> <option class="BMW" value="http://www.google.com"> M5</option> <option class="Audi" value="Audi">Audi</option> <option class="Audi" value="http://www.google.com"> A4</option> <option class="Audi" value="http://www.google.com"> S4</option> </select> <script> $(function() { $("#qmt-vehicle").on("change", function() { var levelClass = $('#qmt-vehicle').find('option:selected').attr('class'); console.log(levelClass); $('#qmt-manufacturer option').each(function() { var self = $(this); if (self.hasClass(levelClass) || typeof(levelClass) == "undefined") { self.show(); } else { self.hide(); } }); }); }); </script> <p align="center"> </td> </tr>
You just need to add this to your script : 您只需将其添加到脚本中:
$("#qmt-manufacturer").on("change",function()
{
window.location.href = $('#qmt-vehicle').find('option:selected').attr("value");
});
You can modify the select tag like this: 您可以像这样修改select标签:
<select id="qmt-manufacturer" name="manufacturer" onchange="location = this.options[this.selectedIndex].value">
(as taken from this question -> using href links inside <option> tag ) (取自这个问题 - > 使用<option>标签内的href链接 )
I know this is not an answer cause @DinoMyte answered it already .. but to simplified your code a lil bit .. use 我知道这不是一个答案,因为@DinoMyte已经回答了它...但是要简化你的代码一点点..使用
$('#qmt-manufacturer').val('').find('> option').hide();
$('#qmt-manufacturer > option[class="'+levelClass+'"]').show();
instead of 代替
$('#qmt-manufacturer option').each(function () {
var self = $(this);
if (self.hasClass(levelClass)|| typeof(levelClass) == "undefined") {
self.show();
} else {
self.hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.