使用jQuery从DropDownList更改页面
我有一个简单的jQuery脚本,它从静态下拉列表中选择当前页面,当选择更改时,脚本还会修改锚标记的href属性以反映导航更改。 这是我的代码:
Insulation Windows Siding Roofing Gutters & Gutter Protection Patio Doors Go! $(document).ready(function () { //get the current page var cPage = ''; //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(); } }); });
我想在此看到的唯一改进是当用户从下拉列表中选择不同的页面时页面自动更改(’自动点击’锚标记)。
提前致谢!
如果我正确理解您的问题,您问的是如何自动重定向到给定的url,而不是需要点击?
使用Javascript,您只需使用window.location
属性重定向浏览器即可。
window.location = '/somepath/someurl.htm';
这将绕过任何按钮点击等的需要。只需将位置设置为下拉列表中选择的任何值。
这是我认为你想要做的更轻薄的版本:
Select Me
试试window.location.replace()
。 它会自动重定向到页面。