【问题标题】:Keep select list choice after refresh刷新后保留选择列表选项
【发布时间】:2012-12-09 14:18:24
【问题描述】:

我有以下代码根据选项的选择值重定向页面(onchange)。 例如我们在 index.php 中。有一个选择列表,如果用户选择值为“one”的设计,则重定向页面将是同一页面但具有不同的 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>

感谢您的宝贵时间

【问题讨论】:

  • 您可以使用$_GET 发送/获取参数,例如index.php&amp;selected=one 并使用 php $_GET['selected'] 获取,然后使用 value="one" 设置适当的选项
  • 您可能想用问号替换 & 符号?

标签: php javascript html


【解决方案1】:

使用 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];
}

请记住,您必须将&amp; 更改为? 并设置参数和值。喜欢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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • $menu_option = $_GET['product'];如果 $_GET['product'] 没有设置,会给你警告。
      • Gaurav 是正确的,我只是给出了一个基本的解决方案,不一定是生产代码。
      【解决方案4】:

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

      我有一个处理这个的函数。 github上的版本有点旧,不过应该还能用。

      $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】:
        <?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>
        

        【讨论】:

          【解决方案6】:

          用这个替换onchange: onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?pro='+this.options [this.options.selectedIndex].value">

          【讨论】:

            猜你喜欢
            • 2021-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-07
            • 1970-01-01
            • 2017-03-23
            • 2014-01-02
            相关资源
            最近更新 更多