Javascript 在更改html select时传递参数

Javascript 在更改html select时传递参数,javascript,html,select,onchange,Javascript,Html,Select,Onchange,假设我有两个html页面,它们都包含相同的选择框: <select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);"> <option value="/site1.php">Site1</option> <option value="/site2.php"&g

假设我有两个html页面,它们都包含相同的选择框:

<select onchange="this.options[this.selectedIndex].value && (window.location =    this.options[this.selectedIndex].value);">
<option value="/site1.php">Site1</option>
<option value="/site2.php">Site2</option>
</select>

地点1
站点2
我正在寻找一种将selectedIndex的值传递到下一页的方法,这样我也可以在第二页的选择框中使用它。有什么建议吗


提前感谢

您可以使用查询参数:

window.location = 'YOUR_URL?selectedIndex=' + this.options[this.selectedIndex].value

使用查询参数强制您解析它们。这一点已经解释过了(纯JS可以做得很好)。

localStorage

商店:

localStorage.userEdits=this.options[this.selectedIndex].value;
在另一页上获取:

var yourIndex = localStorage.userEdits;
散列

设置:

获取:


你可以通过POST、GET、COOKIE、SESSION、LocalStorages传递。问题是,当我通过php传递时,我无法通过javascript设置selectedindex,因为我无法访问php变量。请看我的第一个答案:)这是一个有效的解决方案,但不适用于我的场景,因为该网站将由较旧的Internet浏览器使用。谢谢,但对我来说,这似乎不是一种优雅的方式,因为我还传递了其他$get变量。@Dede1989哦,不适合吗?但它是有效的,没想到你想要一个优雅的地址!
window.location.href="yourURL#index="+this.options[this.selectedIndex].value;
var yourIndex=window.location.hash.replace("index=","").parseInt();