使用网址变量的下拉菜单的动态默认选择 [英] Dynamic default selection for a drop down menu using url variables

查看:121
本文介绍了使用网址变量的下拉菜单的动态默认选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个页面上有几个链接,它们都指向同一个页面,但每个链接都在URL中传递了一个不同的变量(http://sitename.com/thispage.php?id=3)。我将新变量设置为true或false,具体取决于为id传递了哪个值,然后将true / false变量设置为在选项标记上设置默认选项,如下所示:

  if(isset($ _ GET ['id']))
$ id =($ _GET ['id']);

if($ id = 4)
$ id4 = true;
else
$ id4 = false;

...

< option value =Valueselected =<?php echo$ id4;?>>
价值
< / option>

如果看起来很乱,请原谅。

我有12个设置,每个菜单选项一个。我似乎无法得到它显示正确的一个作为默认选择,但它总是选择第二个到最后一个菜单项。

解决方案

div>

你应该纠正的一件事是把真和假放在引号中,否则它们将从布尔转换为字符串并最终以1或0结束。



<你可以用这个替换整个事物

 < option<?php if(isset($ _ GET ['id '])&& $ _GET ['id'] == 4){echo'selected'; }?>> 

< / option>


I have several links on one page which all direct to the same page, but each of them passes a different variable in the URL (http://sitename.com/thispage.php?id=3). I set new variables to true or false depending on which value is passed for "id", then put the true/false variables in to set the default option on an option tag like so:

    if (isset($_GET['id']))
        $id = ($_GET['id']);

    if ($id = 4)
        $id4 = true;
    else
        $id4 = false;

    ...

    <option value="Value" selected="<?php echo "$id4"; ?>">
        Value
     </option>

Excuse me if it seems messy.

I've got 12 of those setup, one for each menu option. I can't seem to get it to display the correct one as the default selection though, as it always selects the 2nd to last menu item.

解决方案

One thing you should correct is putting true and false in quotation marks, otherwise they will be converted from boolean to string and end up as a 1 or 0.

Or you could replace the whole thing with this

<option<?php if (isset($_GET['id']) && $_GET['id'] == 4){ echo ' selected'; } ?>>
    value
</option>

这篇关于使用网址变量的下拉菜单的动态默认选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆