简体   繁体   English

如何在选择中选择最大值

[英]How to have the max value selected inside a select

I have the following: 我有以下几点:

PHP CODE : PHP代码

<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
    <?php
    for($h=1; $h<6; $h++){
        echo '<option value="'.$h.'">'.$h.'</option>';
    }
    ?>
</select>

This prints : 打印

<select name="valoracion0" id="valoracion0">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

When this <select> appears I want by default for the selected value to be the max number. 当出现此<select> ,默认情况下,我希望所选值是最大数量。 How can I do this? 我怎样才能做到这一点?

Like this.. 像这样..

<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
    <?php
    $cnt=6;$sel=""; // <--- Add a variable $sel with nothing assigned
    for($h=1; $h<$cnt; $h++){
        if($h==($cnt-1)) //<-- Here goes the check
        {
            $sel="Selected";
        }
        echo '<option '.$sel.' value="'.$h.'">'.$h.'</option>';
    }
    ?>
</select>

You can try this, 你可以试试看

<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
    <?php
    for($h=1; $h<6; $h++){
       if($h == 5){
         echo '<option value="'.$h.'" selected >'.$h.'</option>'; 
       }else{
        echo '<option value="'.$h.'">'.$h.'</option>';
       }
    }
    ?>
</select>

This should work: 这应该工作:

<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
    <?php
    for($h=1; $h<6; $h++){
        if($h == 5){ $selected = " selected=\"selected\""; }else{ $selected = NULL; }
        echo '<option value="'.$h.'"'.$selected.'>'.$h.'</option>';
    }
    ?>
</select>

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

相关问题 如何将选定的最高和最低价格值传递给html中select标签内的Java脚本函数? - How to pass the selected max and min price value to java script function inside of select tag in html? 如何从一列 select 最大值并使用选定的最大值更新另一列 - How can I select max value from one column and update another column with selected Max value 如何从中选择一个值<select>标签 - How to have a value already selected from <select> tag 如何在选择下拉列表中选择ID类别? - How to have the ID category selected in select dropdown? PHP Mysqli HTML如何从数据库中选择当前值<select> - PHP Mysqli HTML How to have current value from database selected in <select> 如何使用我拥有的 php 值更改 select 属性中的选定选项? - How can I change the selected option in select atribute using the php value that i have? 如何根据从上一个[以上]下拉列表中选择的值来填充select2下拉列表中的选项? - How to fill the options inside a select2 dropdown based on the value selected from the previous[above] dropdown? 如何选择最大值 - How select max value where like 如何在jQuery中设置选择选项的选定值? - How to set the selected value of Select Option in jQuery? 如何使用localStorage获取选定选择的值 - How to get the value of selected select using localStorage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM