简体   繁体   English

如何使用 php 突出显示 select 框中的选项值

[英]How do i highlight the option value in select box using php

I have record set where iam looping through and displaying options in select box if the record contains a value as status as "Y" than i have to display the option in red color如果记录包含状态为“Y”的值而不是我必须以红色显示选项

echo "<select name='select1' size='10' multiple >";
                        foreach ($this->arr['record'] as $rows) {
                            if($rows['status'] == "Y"){
                                echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";
                            }else{
                                echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";
                            }

                        }
echo "</select>";

Here if we have status as "Y" than we are want to highlight the entire option.在这里,如果我们的状态为“Y”,那么我们想要突出显示整个选项。 I used the font tag but it is not working can you please tell us what to do我使用了字体标签,但它不起作用,你能告诉我们该怎么做吗

This has to do with HTML, not PHP.这与 HTML,而不是 PHP 有关。 Set the selected attribute for the <option> .<option>设置selected属性。

In your case:在你的情况下:

if($rows['status'] == "Y") {
  echo '<option selected="selected" ....
}

please just look on the generated html source code.请查看生成的 html 源代码。 Your question is really not related to php.您的问题确实与 php 无关。 Use the source view of your browser or use firebug to determine why your html attributes / css style is not applied.使用浏览器的源代码视图或使用 firebug 来确定您的 html 属性/css 样式未应用的原因。

Derby, you're using the style attribute the wrong way.德比,您使用样式属性的方式错误。 Inside the style attribute you may specify "inline" CSS, just according to the general CSS rules.在样式属性中,您可以根据一般的 CSS 规则指定“内联”CSS。

So to highlight the selected item in red use the following code according to your needs:因此,根据您的需要使用以下代码以红色突出显示所选项目:

echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' style='background-color: red; font-size: 5pt'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']."&nbsp;</option>";

But please note, as Jason already stated, that drop-down boxes are normally handled by the Operating System, so this style MAY apply but there's no official way to style the drop-down elements.但请注意,正如 Jason 已经说过的,下拉框通常由操作系统处理,因此这种样式可能适用,但没有官方方法来设置下拉元素的样式。

More Info on how to use inline style sheets may be found here .更多关于如何使用内联样式表的信息可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM