首页 > 解决方案 > 我的 Bootstrap 不工作,试图包含 Mysql 下拉菜单

问题描述

我正在尝试将 Bootstrap 应用于动态下拉菜单。

我的代码无法匹配所有其他不是 Mysql 下拉菜单的菜单项。是它的样子。

我的代码:

<li class = 'dropdown'>
    <a href = '#' class = 'glyphicon glyphicon-book' >
        <span class = 'caret'></span>
    </a>

    <select 
    onChange="window.location.href=this.value" class = 'dropdown-toggle' 
    data-toggle = 'dropdown'  >
        <ul class = 'dropdown-menu'>
            <option>Training </option>

<?php                                           

     $sql = "SELECT * FROM sessions where event_type = 'training' ORDER BY 
    pro_title ";

   $res = mysqli_query($con1, $sql);

   if(mysqli_num_rows($res) > 0)                                         
   {                                                 
       while($row = mysqli_fetch_array($res))                                        
       {                                         
           echo "<li><option value='mail/success.php?idx={$row['event_id']}'>{$row['pro_title']}</option></li>";                                         
       }                                 
    }
?>
        </ul>           
    </select>                               
<li>

标签: phphtmltwitter-bootstrap

解决方案


<select> 元素必须有 <option> 元素作为子元素。

请参阅<select>本机表单小部件 - 下拉内容

然而,在 Bootstrap 中,DropDowns 是使用 div 元素或列表创建的。

根据您当前的代码,我假设您使用的是 Bootstrap 3.x

这是您可以尝试的示例:

<li class="dropdown">
    <a href="#" class="glyphicon glyphicon-book dropdown-toggle" data-toggle="dropdown" role="button">
        Training <span class='caret'></span>
    </a>

    <ul class="dropdown-menu">
        <?php

        $sql = "SELECT * FROM sessions where event_type = 'training' ORDER BY pro_title ";

        $res = mysqli_query($con1, $sql);

        if (mysqli_num_rows($res) > 0) {
            while ($row = mysqli_fetch_array($res)) {
                echo "<li><a href='mail/success.php?idx={$row['event_id']}'>{$row['pro_title']}</a></li>";
            }
        }
        ?>
    </ul>
<li>

要了解有关该结构的更多信息,我鼓励您查看以下链接(Bootstrap 3.x):

更新

<select onChange="window.location.href=this.value"> does the same as if you would click on the links in the dropdown <a href="somelink">text</a>。_

不过,您可以在单击项目时执行此操作,如下所示:

试试这个代码来回显列表项:

echo "<li><a onclick='window.location.href=\"mail/success.php?idx={$row['event_id']}\">{$row['pro_title']}</a></li>";

推荐阅读