刷新后保持选择列表选择

时间:2011-03-08 13:39:59

标签: php javascript html

我有以下代码根据option的选择值重定向页面(onchange)。 例如,我们在index.php中。有一个选择列表,如果用户选择值为“1”的Design,则重定向页面将是同一页面但具有不同的URL(index.php& one)

我的目标是在重定向/刷新后选择所选的选项。

这是我的代码

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

感谢您的时间

6 个答案:

答案 0 :(得分:0)

在加载页面(或其他页面)时更改和重新加载时将数据存储在cookie中

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分: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

答案 4 :(得分:0)

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

我有一个处理这个问题的函数。

$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);

答案 5 :(得分:0)

用这一个替换onchange: onchange =“if(this.options.selectedIndex&gt; 0)window.location.href ='index.php?pro ='+ this.options [this.options.selectedIndex] .value”&gt;