在下拉列表中选择多个具有复选框的项

jchrr9hc  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(370)

我有一个带有复选框的下拉菜单。我可以通过单击复选框来选择多个项目。但无法通过单击下拉框中的项目名称进行选择。现在,当我点击name而不是checkbox时,光标会移到网页的顶部,在下拉菜单中没有选择任何内容。有没有办法,我可以点击多个下拉菜单项,他们被选中?我的代码如下:

<div id="parents"  style="display:block">   
     <div class="col-sm-5">
      <div class="dropdown test">
         &nbsp;<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" name="single[]" id="single">Select Event Singles
           <span class="caret"></span></button>
                <ul class="dropdown-menu" >
                <?php

     $mysqli = new mysqli("localhost", "user", "passwd", "db_name") or die("could not connect to database") . "<br>";
                                    $result = $mysqli->query("SELECT EventID,Event_Name FROM events where EventID IN (2,3,4,5,6,8,9,10,12,16,17,18,20)");
                                    if ($result->num_rows > 0) {
                                        // output data of each row
                                        while ($row = $result->fetch_assoc()) {

                                            echo "<li><a href='#' class='test'><input type='checkbox' name='frames[]' value='".$row['EventID']."' >" . $row["Event_Name"] . "</input></a></li>";
                                        }

                                    }
                                    $mysqli->close();
                                    ?>
                                </ul>
                            </div>
                        </div>
cnjp1d6j

cnjp1d6j1#

<div id="parents"  style="display:block">   
<div class="col-sm-5">
    <div class="dropdown test">
        &nbsp;<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" name="single[]" id="single">Select Event Singles
        <span class="caret"></span></button>
        <ul class="dropdown-menu" >
        <?php
            $mysqli = new mysqli("localhost", "user", "passwd", "db_name") or die("could not connect to database") . "<br>";
            $result = $mysqli->query("SELECT EventID,Event_Name FROM events where EventID IN (2,3,4,5,6,8,9,10,12,16,17,18,20)");
            if ($result->num_rows > 0) {
                // output data of each row
                while ($row = $result->fetch_assoc()) {

                    echo "<li><input type='checkbox' name='frames[]' value='".$row['EventID']."' id='checkbox_".$row['EventID']."'></input><label for='checkbox_".$row['EventID']."'>" . $row["Event_Name"] . "</label></li>";
                }

            }
            $mysqli->close();
            ?>
        </ul>
    </div>
</div>

你可以试试这个。
您可以为每个复选框分配id。
为每个复选框文本添加标签。
为每个标签添加“for”属性,并指定相应复选框的id。

相关问题