开发者

Why is'nt this JavaScript function not redirecting to selected option value?

开发者 https://www.devze.com 2023-01-31 23:00 出处:网络
I have a JavaScript function: function redirect(location) { window.location.href=开发者_开发百科location;

I have a JavaScript function:

function redirect(location) {
    window.location.href=开发者_开发百科location;
}

Which I'm using like so:

<select onChange="redirect(this.options[this.selectedIndex].value)">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>

I'm expecting for it to redirect to the selected option value, but does'nt seem to do anything? - bare in mind im new to JavaScript.

Hope someone can help! :)


Try this:

<select onChange="redirect(this.value)">


try this

<script>
function redirect() {
    var location=document.getElementById("i").value;
    window.location.href=location;
}

</script>
<select onChange="redirect()" id="i">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>


Your code itself is functional (i tried).

Where have you placed the function definition? Is there any other javascript code on the page?

In which browser are you testing this? If you're running it in Firefox check the error console (Tools->error console).

I also noticed that Internet Explorer 8 blocks this javascript, I haD to check allow blocked content for this to work.


can be because location is a short way to call window.location, so just rename your input parameter - _location, newLocation, loc, whatever else.

UPDATE

Nope, this should work. Having only

<select onChange="redirect(this.options[this.selectedIndex].value)">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>
<script type="text/javascript">
function redirect(location) {
    window.location.href=location;
}
</script>

on the page gave me the desired result, so the error may be somewhere around - just give us more code!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号