刷新后保持选择列表选择 [英] Keep select list choice after refresh
问题描述
我有以下代码根据option的选择值重定向页面(onchange)。
例如我们在index.php中。有一个选择列表,如果用户选择值为1的Design,则重定向页面将是同一页面但具有不同的URL(index.php& one)
I have the following code that redirects the page (onchange) based on the value of option's choice. For example we are in index.php. There is a select list and if the user chooses Design that has value "one" the redirected page will be the same page but with different url ( index.php&one )
我的目标是在重定向/刷新后选择所选的选项。
My goal is to have the selected option, selected after the redirection/refresh.
这是我的代码
<select size="1" name="Products"
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php&'+this.options [this.options.selectedIndex].value">
<option value="">Please Select a Product</option>
<option value="one">Design
Software</option>
<option value="two">Manufacturing
Software</option>
<option value="three">Machine Tools</option>
</select>
感谢您的时间
推荐答案
使用javascript和jQuery框架的解决方案:
A solution with javascript and jQuery framework:
$(document).ready(function(){
var option = gup('option');
$("select option[value='"+option+"']").attr('selected','selected');
});
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
请注意,您必须更改& ;
到?
并设置参数和值。喜欢选项=一个
。
Keep in mind that you have to change the &
to ?
and set a parameter and a value. like option=one
.
因此相应的javascript必须更改为:
So the corresponding javascript must change to this:
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?=option'+this.options [this.options.selectedIndex].value">
这篇关于刷新后保持选择列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!