html选择列表选择项目和最大值可以选择

时间:2011-04-26 12:07:13

标签: html selectlist

您好, 我有一个html选择列表,我希望:所选数字为给定值$content->number,如果小于5,则最大值为$content->product_type->stock_2,如果大于5,则为5 5。

现在我有:

<select class="number" name="number">
    <? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?>
        <option value="<?= $content->number ?>"><?= $content->number; ?> </option>                      
    <? for ($i = 1; $i <= $max; $i++):?>                        
        <option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option>            
    <? endfor; ?>
</select>

但是它显示了所选值$content->number的两倍。我确定我在某处误会。

有什么建议吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道我是否理解你想做什么,但试试这个:

<select class="number" name="number">
    <? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?>
        <option value="<?= $content->number ?>"><?= $content->number; ?> </option>     
    <? for ($i = 1; $i <= $max; $i++):?>
        <? if ($i != $content->number): ?>                        
            <option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option> 
        <? endif; ?>           
    <? endfor; ?>
</select>