更改使用jQuery的DropDownList从一个页面 [英] Changing a page from DropDownList using jQuery
问题描述
我有一个简单的jQuery脚本,从静态的dropdownlist选择当前页面,当选择更改脚本还修改一个锚标记的href属性,以反映导航的变化。这里是我的code:
I have a simple jQuery script that selects the current page from a static dropdownlist, and when the selection changes the script also modifies the href attribute of an anchor tag to reflect the navigation change. Here is my code:
<select name="PageSelectDropDown" id="PageSelectDropDown">
<option value="Insulation">Insulation</option>
<option value="Windows">Windows</option>
<option value="Siding">Siding</option>
<option value="Roofing">Roofing</option>
<option value="Gutters">Gutters & Gutter Protection</option>
<option value="PatioDoors">Patio Doors</option>
</select>
<a href="" id="clicker">Go!</a>
<script type="text/javascript">
$(document).ready(function () {
//get the current page
var cPage = '<%= ViewData["CurrentPage"] %>';
//select the current page from the list
$("#PageSelectDropDown > option").each(function () {
if ($(this).val().toLowerCase() == cPage.toLowerCase()) {
$(this).attr("selected", "selected");
}
});
//change the link target
$("#PageSelectDropDown").change(function () {
var str = "";
$("#PageSelectDropDown option:selected").each(function () {
str += $(this).val() + " ";
});
$("#clicker").attr("href", "/Product/" + str.trim());
if (cPage != str.trim()) {
$("#clicker").click();
}
});
});
</script>
我想看到这个唯一的改进是将自动更改(自动点击锚标记),当用户从DropDownList中选择一个不同的页面。
The only improvement I would like to see on this is for page to automatically change ('auto-click' anchor tag) when the user selects a different page from the dropdownlist.
在此先感谢!
推荐答案
如果我正确地理解你的问题,你问你怎么自动重定向,而不必点击需要给定的URL,?
If I understand your question correctly, you are asking how do you automatically redirect to the given url, instead of having a click needed?
用JavaScript,你可以简单地使用 window.location的
属性来重定向浏览器。
With Javascript, you can simply use the window.location
property to redirect the browser.
window.location = '/somepath/someurl.htm';
这将绕过任何需要点击一个按钮,就等设置的位置的任何值在下拉菜单中选择。
This will bypass any need for a button click, etc. Just set the location to whatever value is selected in the dropdown.
这篇关于更改使用jQuery的DropDownList从一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!