doumou1864 2010-11-16 22:11
浏览 60

记住下拉选择

I've got a few select lists on my form which look a bit like this:

<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>&pound;400,000</option>

As you can see (above) I've got a bit of PHP in there telling the form to remember it's selection upon submit.

Is there a short bit of PHP that would remember every selection without the rather heavy method I'm using above?

Thanks in advance.

UPDATE:

<select name="MaxPrice" id="MaxPrice">
<option value="9999999" selected>Price (Max)</option>
<option value="100000" <?php if($_GET['MaxPrice'] == "100000") { echo("selected"); } ?>>&pound;100,000</option>
<option value="200000" <?php if($_GET['MaxPrice'] == "200000") { echo("selected"); } ?>>&pound;200,000</option>
<option value="300000" <?php if($_GET['MaxPrice'] == "300000") { echo("selected"); } ?>>&pound;300,000</option>
<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>&pound;400,000</option>
</select>

UPDATE 2: Is there a way to implement this technique into some JavaScript?

if (BuyRent=='buy')
document.SearchForm.MaxPrice.options[9999999]=new Option("Price (Max)","150000000");
document.SearchForm.MaxPrice.options[100000]=new Option("\u00A3100,000","100000");
document.SearchForm.MaxPrice.options[200000]=new Option("\u00A3200,000","200000");
document.SearchForm.MaxPrice.options[300000]=new Option("\u00A3300,000","300000");
document.SearchForm.MaxPrice.options[400000]=new Option("\u00A3400,000","400000");    
  • 写回答

2条回答 默认 最新

  • donglu9978 2010-11-16 22:15
    关注

    Usually, you use some kind of loop. Here's an example :

    $my_values = array(100000, 200000, 300000, 400000);
    
    foreach($my_values as $value) {
        echo "<option value=\"{$value}\"";
        echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"';
        echo ">" . number_format($value, 0, '.', ',') . "</option>";
    }
    

    Which would print 4 tags such as <option value="100000" selected="selected">100,000</option>.

    This code also uses the ternary operator, but it's obviously not mandatory, you can write the whole if/else statement if wanted.

    Edit:

    <select name="MaxPrice" id="MaxPrice">
        <option value="9999999">Price (Max)</option>
        <?php
        $my_values = array(100000, 200000, 300000, 400000);
    
        foreach($my_values as $value) {
            echo "<option value=\"{$value}\"";
            echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"' : "";
            echo ">&pound; " . number_format($value, 0, '.', ',') . "</option>";
        }
        ?>
    </select>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值