Use*_*007 3 html javascript redirect window.location
我试图根据下拉框的值动态重定向页面.我在JavaScript中获得了下拉框的值.根据下拉值,我想重定向页面.
这是示例代码:
<script type="text/javascript">
function RedirectMe(){
var chosanDept = document.getElementById("Dept");
var str = chosanDept.options[chosanDept.selectedIndex].text;
if(str=='HR')
{
alert('Yes in IF' + str);
window.location = "http://www.google.com";
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这
chosanDept是获取下拉框值的变量.但我无法使用window.location,location.replace,location.href等各种函数重定向页面.还有一个我的if条件有效,我收到警报'在IF HR中是'
这里出了什么问题?
尝试添加return false;到RedirectMe()功能的末尾
然后无论你在哪里调用这个函数,都要确保你放在return那里,比如onclick="return RedirectMe();"