由php填充的下拉菜单显示的选项数量有限

问题描述 投票:0回答:2

我的下拉菜单如下:

   <select onchange="random_function()" id='choice' >
      <option selected="selected" >
         Make a choice
      </option>
  </select>

我通过<select>标记内的php请求填充它,如下所示:

<?php
    include 'config.php';
    $query = $pdo->query('
                SELECT choices
                FROM allTheChoices'   
    );
    while ($row = $query->fetch()){
       echo "<option>".$row['choices']."</option>";
    }
?>

目前,我的数据库尚不完整,只有两个选项可以显示,但将来会有很多(超过100个)。

我的目标是找到一种方法,使选择项仅显示10个选项,并且应该显示滚动条以查看所有其余选项。

我已经尝试过使用<select size="10">,但是下拉菜单的设计已完全更改并且很糟糕。您是否有一种简单的方法来执行此操作而不更改设计?

php jquery html css
2个回答
1
投票

使用了这段代码来实现我想要的:

 <select  style="position:absolute;" onmousedown="if(this.options.length>8)
{this.size=8;}"  onchange='this.size=0;' onblur="this.size=0;" >

选择选项时,请确保使用以下内容避免容器div扩展:

style="position:absolute;"

并用您要显示的选择数量编辑以下代码(在此示例中使用10):

onmousedown="if(this.options.length>10){this.size=10;}"

所以我使用的整个html代码是:

  <select  style="position:absolute;" onmousedown="if(this.options.length>8)
    {this.size=8;}"  onchange='this.size=0;' onblur="this.size=0;" >
          <option selected="selected" >
             Make a choice
          </option>
  </select>

0
投票

尝试在您的SQL语句中使用限制,例如:“ LIMIT 10”。

所以这就是代码的外观:

<?php
include 'config.php';
$query = $pdo->query('
            SELECT choices
            FROM allTheChoices
            LIMIT 10' 
);
while ($row = $query->fetch()){
   echo "<option>".$row['choices']."</option>";
} ?>
© www.soinside.com 2019 - 2024. All rights reserved.