Javascript 如何使用Javascript在浏览器的“后退”按钮上重置下拉列表<select>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4287408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:44:52  来源:igfitidea点击:

How to reset dropdown list <select> on 'back' button of browser using Javascript?

javascripthtml

提问by code master

How to reset (back to 0 index) dropdown list on 'back' button of browser using Javascript?

如何使用Javascript重置(回到0索引)浏览器“后退”按钮上的下拉列表?

采纳答案by code master

<script type="text/javascript" language="javascript" >

var orig_default = -1;

function setDefault() {
if (orig_default < 0) {orig_default = document.getElementById("MyList").selectedIndex;}
}

function testReset() {
if (orig_default >= 0) {
    document.getElementById("MyList").selectedIndex = orig_default;
 }
}
</script>
<body onbeforeunload='setDefault();'>

Here are couple of scripts which I have written myself to solve the issue of resetting the value of html upon browser back button click.

这是我自己编写的几个脚本,用于解决在单击浏览器后退按钮时重置 html 值的问题。

回答by Don

You can use the 'onbeforeunload' event:

您可以使用 'onbeforeunload' 事件:

<script>
function reset_options() {
    document.getElementById('MySelect').options.length = 0;
    return true;
}

</script>
<body onbeforeunload='reset_options()'>
...

回答by pearl's

yes its easy.

是的,很容易。

i have a dropbox and its id was "stroke_style" the reset is just to set it at -1 index (before 0)

我有一个保管箱,它的 ID 是“stroke_style”,重置只是将它设置为 -1 索引(0 之前)

document.getElementById("stroke_style").selectedIndex = -1;

example: http://languagelassi.blogspot.in/search/label/Dropdown%20Reset%20Javascript

示例:http: //languagelassi.blogspot.in/search/label/Dropdown%20Reset%20Javascript

and you can put it in a method or directly <body onload="">of that page

你可以把它放在一个方法中或直接放在<body onload="">那个页面上