2011-03-08 89 views
1

我有以下代码根据选项的值选择重定向页面(onchange)。 例如我们在index.php。有一个选择列表,如果用户选择具有值“一”的设计,重定向页面将是相同的页面,但具有不同的URL(index.php &之一)刷新后保留选择列表选项

我的目标是让选定的选项重定向/刷新后。

这里是我的代码

<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> 

感谢您的时间在变化饼干

+0

您可以发送/获取参数w /'$ _GET' eg 'index.php&selected = one'并取得w/php'$ _GET ['selected']',然后设置相应的选项w /'value =“one”' – kjy112 2011-03-08 13:43:37

+2

您可能想要用&符号替换&符号? – Simeon 2011-03-08 13:44:09

回答

0

溶液与JavaScript和jQuery框架:

$(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]; 
} 

请记住,你必须改变&?和设置的参数和值。像option=one

所以相应的JavaScript必须改成这样:

onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?=option'+this.options [this.options.selectedIndex].value"> 

演示:http://lb.vg/969f6

0

存储数据,并重新加载页面时(或其他页面)加载

0

你需要将变量与重定向一起传递。然后,将其从$ _GET变量中取出,并检查构建select时是否设置了该值。

<?php 
$menu_option = $_GET['product']; 
?> 
<select size="1" name="Products" 
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?product='+this.options [this.options.selectedIndex].value"> 
<option value="">Please Select a Product</option> 
<option value="one"<?php if ('one' == $menu_option) { echo ' selected="selected"; } ?>>Design 
Software</option> 
<option value="two"<?php if ('two' == $menu_option) { echo ' selected="selected"; } ?>>Manufacturing 
Software</option> 
<option value="three"<?php if ('three' == $menu_option) { echo ' selected="selected"; } ?>>Machine Tools</option> 
</select> 
+0

$ menu_option = $ _GET ['product'];如果$ _GET ['product']没有设置,会给你警告。 – Gaurav 2011-03-08 13:49:11

+0

Gaurav是正确的,我只是给出了一个基本的解决方案,不一定是生产代码。 – skwigger 2011-03-11 15:23:32

0
<?php 
$selected = 0; 
if(isset($_GET['Products'])) 
    $selected = $_GET['Products']; 

?> 

<select size="1" name="Products" 
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?Products='+this.options[this.options.selectedIndex].value"> 
<option value="">Please Select a Product</option> 
<option value="one" <?php echo ('one' == $selected) ? 'selected="selected"' : '';?> >Design 
Software</option> 
<option value="two" <?php echo ('two' == $selected) ? 'selected="selected"' : '';?>>Manufacturing 
Software</option> 
<option value="three" <?php echo ('three' == $selected) ? 'selected="selected"' : '';?>>Machine Tools</option> 

+0

seletecf是一个正确的参数? – EnexoOnoma 2011-03-08 14:01:56

+0

@Panagios:谢谢。这是一个错字。 – Gaurav 2011-03-08 14:03:09

0

https://github.com/paulschreiber/misc/blob/master/php/popup_menu.php

我处理这个功能。 github上的版本有点旧,但仍然可以工作。 平变化=“如果(this.options.selectedIndex> 0)window.location.href = '?的index.php亲=' + this.options [this.options:

$productList = array (
"one" => "Design Software" 
"two" => "Manufacturing Software" 
"three" => "Machine Tools" 
); 

$js = "onchange=\"if(this.options.selectedIndex>0) window.location.href = 'index.php?Products='+this.options[this.options.selectedIndex].value\""; 

print popup_menu("Products", $productList, $js); 
0

与这一个替换平变化。 selectedIndex] .value“>