admin管理员组

文章数量:1353694

i'm writing some inline javascript. it's not working and i'm not sure why.

at the top of the index page

<meta http-equiv="Content-Script-Type" content="text/javascript" />

later on i have:

<select name='id'> 
<option value=-1>New Entry</option> 
<option value='1' onclick="location.replace('index.php?page=update&id=1')">2010-06-12 16:38:08</option> 
<option value='2' onclick="location.replace('index.php?page=update&id=2')">2010-06-12 18:20:49</option> 
<option value='3' onclick="location.replace('index.php?page=update&id=3')">2010-06-13 11:39:09</option> 
</select>

what i want is for the page to be replaced when one of the option items is selected but the code is not causing a page refresh and i'm not sure why. is something wrong with the javascript?

i'm writing some inline javascript. it's not working and i'm not sure why.

at the top of the index page

<meta http-equiv="Content-Script-Type" content="text/javascript" />

later on i have:

<select name='id'> 
<option value=-1>New Entry</option> 
<option value='1' onclick="location.replace('index.php?page=update&id=1')">2010-06-12 16:38:08</option> 
<option value='2' onclick="location.replace('index.php?page=update&id=2')">2010-06-12 18:20:49</option> 
<option value='3' onclick="location.replace('index.php?page=update&id=3')">2010-06-13 11:39:09</option> 
</select>

what i want is for the page to be replaced when one of the option items is selected but the code is not causing a page refresh and i'm not sure why. is something wrong with the javascript?

Share Improve this question edited Jun 19, 2010 at 18:12 moonlightcheese asked Jun 19, 2010 at 18:05 moonlightcheesemoonlightcheese 10.6k9 gold badges50 silver badges80 bronze badges 2
  • That seems to work for me in Firefox. I presume you are testing in IE? I'm not sure that supports option.onclick based on here bytes./topic/javascript/answers/… – Martin Smith Commented Jun 19, 2010 at 18:09
  • was using chrome. this does work in firefox... didn't realize. thanks for the tip. – moonlightcheese Commented Jun 19, 2010 at 18:15
Add a ment  | 

2 Answers 2

Reset to default 4

Use the onchange event of the select element:

<select name="id" onchange="window.location.replace('index.php?page=update&id='+this.options[this.selectedIndex].value);"> 
<option value="-1">New Entry</option> 
<option value="1">2010-06-12 16:38:08</option> 
<option value="2">2010-06-12 18:20:49</option> 
<option value="3">2010-06-13 11:39:09</option> 
</select>

Note: The location.replace method is used when you want to navigate to the page and also replace the current page in the browsing history. If you just want to navigate to the page normally, you assign the URL to the window.location.href property instead.

I've found that window.location.href doesn't work at all in newer browsers (FF4, Chrome 10, etc.).

本文标签: javascript onclickquotlocationreplace(39url39)quot not workingStack Overflow