douxun2023 2019-04-21 18:32
浏览 103

如何从html下拉菜单中获取/传递所选项目?

I'm trying to obtain the selected menu item from a ul dropdown list, but I'm not sure how to use PHP or JS to obtain the selected item.

The selected item needs to be submitted to the server-side with PHP. I know how to do it with a menu, but I need to use a ul with li items for my current CSS to work.

<div class="TypeListBox">
    <div id="dd" class="wrapper-dropdown-3" tabindex="1">
        <span>Food</span>
            <ul class="dropdown">
                <li><a href="#"><i class="fas fa-pizza-slice"></i></i>Pizza</a></li>
                <li><a href="#"><i class="fas fa-pepper-hot"></i></i>Mexican</a></li>
                <li><a href="#"><i class="fas fa-hamburger"></i></i>Burgers</a></li>

            </ul>
    </div>
</div>

I expect the ability to pass the selected item i.e. "pizza". How do I solve this problem?

  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2019-04-21 18:37
    关注

    You can use this code snippet

     <div class="TypeListBox">
       <div id="dd" class="wrapper-dropdown-3" tabindex="1">
        <span>Food</span>
            <ul class="dropdown">
                <li><a href="javascript:void(0);" onclick="functionABC('Pizza')"><i class="fas fa-pizza-slice"></i></i>Pizza</a></li>
                <li><a href="javascript:void(0);" onclick="functionABC('Mexican')"><i class="fas fa-pepper-hot"></i></i>Mexican</a></li>
                <li><a href="javascript:void(0);" onclick="functionABC('Burgers')"><i class="fas fa-hamburger"></i></i>Burgers</a></li>
            </ul>
       </div>
    </div>
    <script type="text/javascript">
         function functionABC(selectedElement){
            alert(selectedElement);
        }
    </script>
    

    You can use an input type hidden field to track the value of selected

    <input type="hidden" name="selected" id="selected" value="" />
    

    and change the selected value inside function functionABC

    document.getElementById("selected").value = selectedElement;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊