在页面重新加载时保留下拉选择
Retaining dropdown selection on page reload
我在这样的页面上有一个选择列表:
<select name='elements'>
<option value='water'>Water</option>
<option value='fire'>Fire</option>
<option value='air'>Air</option>
</select>
编辑:当用户进行选择时,当前页面将刷新,但是如何在页面刷新后使用户的选择在列表中可见?换句话说,我不希望列表回滚到其默认的选定值。
请帮忙
这是一个仅限 Javascript 的解决方案。
<select name='elements' id='elements' onChange='window.location="yoururl.html?value=" + this.value;'>
<option value='water'>Water</option>
<option value='fire'>Fire</option>
<option value='air'>Air</option>
</select>
然后你用这个函数查看value
参数是否设置并获取它,你可以用jQuery把它设置为selected
。
$("#elements option[value='" + result_of_gup + "']").attr("selected","selected") ;
<select name='elements' onChange='window.location="yoururl.php?value=" + this.value;'>
<option value='water'>Water</option>
<option value='fire'>Fire</option>
<option value='air'>Air</option>
</select>
在没有 jQuery 的情况下执行此操作:
创建window.location.getParameter()
函数(参见 Anatoly Mironov 的此处):
<select name='elements' id='elements' onChange='window.location="yoururl.html?elements=" + this.selectedIndex;'>
<option value='water'>Water</option>
<option value='fire'>Fire</option>
<option value='air'>Air</option>
</select>
<script type="text/javascript">
els = document.getElementById('elements');
selIndex = window.location.getParameter('elements') || 0;
els[selIndex].selected = true;
</script>
为了便于参考,我在下面重现了Anatoly的出色.getParameter
功能:
window.location.getParameter = function(key) {
function parseParams() {
var params = {},
e,
a = /'+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.search.substring(1);
while (e = r.exec(q))
params[d(e[1])] = d(e[2]);
return params;
}
if (!this.queryStringParams)
this.queryStringParams = parseParams();
return this.queryStringParams[key];
};
相关文章:
- 提交后保留下拉选择的值
- 数据是否等于保留或无法被 jQuery/Sizzle 选择
- jQuery blimp文件上传-上传后保留选择图像行
- 保留动态创建的下拉列表's在事件窗口.history.back()上选择的值-JavaScript
- 保留选项列表以供选择
- Yii - 下拉不会保留更改后选择的值
- 余烬.当用户刷新网站时,保留下拉菜单中的选择
- 如何在更改数据源后保留 UI 网格选择
- 我想使用 javascript 将第一个链接保留为默认选择
- 如何在提交查询后保留选择状态
- 在页面重新加载时保留下拉选择
- 使用jQuery UI的选择菜单保留图片
- 将复选框选择保留在多页分页中
- 选择2个自定义匹配器,但保留带状音调符号
- 在tinyMCE问题中单击自定义按钮后保留选择
- 无法保留选择框的值
- 保留“选择多个”框中的值
- 返回和保留选择的下拉列表函数
- 使用range保留选择中的换行符和回车符?Javascript / jquery
- Javascript/jquery:保留选择框的选项项