如何在选择标签中的单个选项上添加链接

你好我正在尝试创建一个类别选择下拉列表,其中包含来自数据库的一些数据我创建了一个名为addcategory.php的页面,我正在尝试在选择标签中添加一个类似“添加新类别”的内容我尝试了几种方法,例如javascript 中的 onchange 函数,但它应用于整个选择菜单我只想添加一个链接到下面的选择标签的最后一个选项我附加了我正在使用的代码。


<select class="w-100" id="categoryselect" name="selectcat" required>

  <option value="" disabled selected>Select Category</option>

    <?php 

        $sql="SELECT * FROM categories";

        $result=mysqli_query($conn, $sql);

        while($row=mysqli_fetch_array($result))

        {

            echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';

        } 

    ?>

  <option value="categories" onClick="window.location = 'addcategory.php'">Add New Category</option>

</select>


波斯汪
浏览 135回答 1
1回答

拉丁的传说

将事件处理程序绑定到 select 标签,而不是将事件绑定到 options 标签。onchange 事件在元素的值发生更改时发生。<select class="w-100" id="categoryselect" name="selectcat" onChange="if(this.value=='categories') window.location='addcategories.php'">&nbsp; &nbsp; &nbsp; <option value="" disabled selected>Select Category</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql="SELECT * FROM categories";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result=mysqli_query($conn, $sql);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row=mysqli_fetch_array($result))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; <option value="categories">Add New Category</option>&nbsp; &nbsp; </select>
打开App,查看更多内容
随时随地看视频慕课网APP