admin管理员组文章数量:1391969
Ok here's the deal, I'm trying to have a switch statement redirect to another page depending on what option is selected in a select box. Below is a sample i'm working with and trying to figure out, any help at all would be greatly apreciated.
<html>
<body>
<form name="form1">
<select name="select1">
<option value="p1">p1</option>
<option value="p2">p2</optino>
</select>
</form>
<script type="text/javascript">
<!--
var sel = document.form1.select1;
var txt = sel.options[sel.selectedIndex].text;
var opt = sel.options[sel.selectedIndex].value;
switch (sel)
{
case 'p1': window.location = ''
break;
case 'p2': window.location = '.'
break;
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Ok here's the deal, I'm trying to have a switch statement redirect to another page depending on what option is selected in a select box. Below is a sample i'm working with and trying to figure out, any help at all would be greatly apreciated.
<html>
<body>
<form name="form1">
<select name="select1">
<option value="p1">p1</option>
<option value="p2">p2</optino>
</select>
</form>
<script type="text/javascript">
<!--
var sel = document.form1.select1;
var txt = sel.options[sel.selectedIndex].text;
var opt = sel.options[sel.selectedIndex].value;
switch (sel)
{
case 'p1': window.location = 'http://www.yahoo.'
break;
case 'p2': window.location = 'http://www.google..'
break;
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Share
Improve this question
edited Dec 7, 2012 at 7:24
James A Mohler
11.1k15 gold badges50 silver badges76 bronze badges
asked Sep 10, 2010 at 18:33
NewBNewB
1253 silver badges13 bronze badges
0
4 Answers
Reset to default 4I remend you avoid switch-statements. How about this?
<select name="select1">
<option data-url="http://www.yahoo." value="p1">p1</option>
<option data-url="http://www.google." value="p2">p2</option>
</select>
And:
document.getElementById("select1").onchange = function () {
var url = this.options[this.selectedIndex].getAttribute("data-url");
window.location = url;
};
Edit: I changed the example to use html 5 data attributes (not to worry, they are 100% supported in all browsers), since you need the value attribute for something else.
- You need to bind an event handler
- Your switch was using the wrong value
See http://www.jsfiddle/vDFpZ/
<html>
<body>
<form name="form1">
<select name="select1">
<option value="p1">p1</option>
<option value="p2">p2</option>
</select>
</form>
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("select1")[0].onchange = function () {
var sel = this.options[this.selectedIndex];
var text = sel.text;
var val = sel.value;
switch (val)
{
case 'p1': window.location = 'http://www.yahoo.'
break;
case 'p2': window.location = 'http://www.google..'
break;
}
}
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
i noticed that you did not bind an change handler to the select, so when the user selects a value nothing happens.
You've got some typos, your second "/option" and your "google.". And the switch statement has nothing to do with your error. You have no event listener/onchange handler and you aren't switching with the value from the select option
https://developer.mozilla/en/DOM/element.onchange https://developer.mozilla/en/DOM/element.addEventListener
本文标签: javascriptswitch statement with RedirectStack Overflow
版权声明:本文标题:javascript - switch statement with Redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707721a2620940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论