简体   繁体   English

在onselect的html select上传递参数

[英]Passing parameters on onChange of html select

lets say i have got two html pages which both contain the same select box: 可以说我有两个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>

I am looking for a way to pass the value of selectedIndex to the next page, so I can use it at the second page select box too. 我正在寻找一种将selectedIndex的值传递到下一页的方法,因此我也可以在第二页的选择框使用它。 Any suggestions? 有什么建议么?

Thanks in advance 提前致谢

localStorage localStorage的

Store: 商店:

localStorage.userEdits=this.options[this.selectedIndex].value;

Get, on the other page : 在另一页上获取:

var yourIndex = localStorage.userEdits;

hash 哈希

Set: 组:

window.location.href="yourURL#index="+this.options[this.selectedIndex].value;

Get: 得到:

var yourIndex=window.location.hash.replace("index=","").parseInt();

You can use query parameters: 您可以使用查询参数:

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

Using query params forces you to parse them. 使用查询参数会强制您解析它们。 This is explained here (you can do just fine with pure JS). 这说明在这里 (你可以做纯JS罚款)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM