简体   繁体   English

从循环中预选

[英]pre-select from loop

So I made this car statistics calculation module, which after doing some calculations returns to show the results and then show the car I've jsut calculated for as the pre-selectedon in my form. 因此,我制作了这个汽车统计数据计算模块,在进行了一些计算之后,该模块将返回以显示结果,然后在我的表单中显示我为之选择的汽车。

For this I made a function in php, I could use within my html: 为此,我在php中创建了一个函数,可以在html中使用:

function biler () {                         
    while($row = mysql_fetch_array($result)) {
       if ($row['reg'] == $_GET['bil']){
           $selected = 'selected="selected"';
       }                    
       echo '<option '.$selected.' value="'.$row['reg'].'">'.$row['reg'].'</option>';
    }
}

The HTML in which the functions run: 运行函数的HTML:

<form method="post" name="regnskab" action="regnskab_beregn.php">
   <select name="bil"><? biler(); ?></select>
</form>

My problem is that the is no car pre selected, because they all have the select statement :/ 我的问题是没有预先选择汽车,因为它们都具有select语句:/

Returns: 返回值:

<select name="bil">
  <option selected="selected" value="OHO1241">OHO1241</option>
  <option selected="selected" value="QTX2314">QTX2314</option>
  <option selected="selected" value="QW20301">QW20301</option>
</select>

You need to clear the $selected variable after using it otherwise it will always contain that value: 您需要在使用完$ selected变量后清除它,否则它将始终包含该值:

while($row = mysql_fetch_array($result)) {
   $selected = "";
   if ($row['reg'] == $_GET['bil']){
       $selected = 'selected="selected"';
   }                    
   echo '<option '.$selected.' value="'.$row['reg'].'">'.$row['reg'].'</option>';
}

Once your $selected is set, it still exists. 设置了$selected ,它仍然存在。 My example 我的例子

    function biler () {                         
        while($row = mysql_fetch_array($result)) {
           if ($row['reg'] == $_GET['bil']){
               $selected = 'selected="selected"';
           }          
           else
           {
               $selected = "";
           }          
           echo '<option '.$selected.' value="'.$row['reg'].'">'.$row['reg'].'</option>';
        }
    }

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

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