简体   繁体   English

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

[英]how to add a link on single option in select tag

hello I am trying to create a category select dropdown which consists of some data coming from database I have created a page named addcategory.php and I am trying to add a in the select tag something like "add new category" I tried several methods like onchange function in javascript but it gets applied to the whole select menu I only want to add a link to the last option of the select tag below I am attaching the code which I am using.您好,我正在尝试创建一个类别 select 下拉列表,其中包含来自数据库的一些数据我创建了一个名为 addcategory.php 的页面,并且我正在尝试在 Z99938282F0407185941E18 onchange function in javascript but it gets applied to the whole select menu I only want to add a link to the last option of the select tag below I am attaching the code which I am using.

<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>

Bind the event handler to select tag instead of bind the event into options tag.将事件处理程序绑定到 select 标记,而不是将事件绑定到选项标记。

The onchange event occurs when the value of an element has been changed. onchange 事件在元素的值已更改时发生。

<select class="w-100" id="categoryselect" name="selectcat" onChange="if(this.value=='categories') window.location='addcategories.php'">
      <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">Add New Category</option>
    </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM