简体   繁体   English

从下拉列表中选择一个列表项以打开模式? 我应该使用jquery / js吗?

[英]Select a list item from dropdown to open modal? Should I use jquery/js?

I'm trying create a dropdown with a few list items, and when a user selects a certain "time" for example "later", triggering the later modal to open. 我正在尝试创建一个包含一些列表项的下拉列表,并且当用户选择某个“时间”(例如“以后”)时,触发稍后的模式打开。 (The original code is select/option but I have javascript converting it to ul/li for styling purposes.) (原始代码是select / option,但是我通过javascript将其转换为ul / li以进行样式设置。)

Here is the the list item code. 这是列表项代码。

<ul class="select-options" style="display: none;">
   <li value="asap">Delivery ASAP</li>
   <li value="later">Delivery Later</li>
   <li value="catering">Catering</li>
</ul>

Here is the modal code. 这是模态代码。

    <div class="later-modal">
        <p>Select a time to deliver.</p>
    </div>

<div class="catering-modal">
        <p>Select a time to cater.</p>
    </div>

Should I use an if/else statement to open a modal? 我应该使用if / else语句打开模式吗? I'm not really sure what to do at this point. 我现在不确定该怎么办。 I'm also not using any bootstrap of any kind. 我也没有使用任何类型的引导程序。

Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

Iam sorry before I have copy bad block of code...try this 我很抱歉,在我复制错误的代码块之前...尝试一下

 $(document).ready(function() { $("#later").click(function() { $(".later-modal").toggle(); }); $("#catering").click(function() { $(".catering-modal").toggle(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="select-options" style="display: block;"> <li value="asap">Delivery ASAP</li> <li id="later" value="later">Delivery Later</li> <li id="catering" value="catering">Catering</li> </ul> <div class="later-modal" style="display: none;"> <p>Select a time to deliver.</p> </div> <div class="catering-modal" style="display: none;"> <p>Select a time to cater.</p> </div> 

I converted the drop-down to a real drop-down (select element). 我将下拉列表转换为实际的下拉列表(选择元素)。

Here is the solution: 解决方法如下:

 $('li').click(function(){ $('.modal').hide(); $('.' + $(this).attr('data-value') + '-modal').show(); $('#dropdown').removeAttr('checked'); }); 
 label { cursor:pointer; border:1px solid silver; border-radius:5px; padding:2px; } ul { list-style:none; padding:2px; margin:0; border:1px solid silver; border-radius:5px; position:absolute; } li { margin:0; cursor:pointer; } #dropdown { display:none; } .select-options { opacity:0; transition:all .2s ease; } #dropdown:checked + .select-options { opacity:1; } .modal { display:none; background:rgba(0,0,0,0.8); position:absolute; top:50%; left:50%; transform:translate(-50%, -50%); background:#000; color:#fff; padding:10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for="dropdown">Choose time</label> <input type="checkbox" id="dropdown" /> <ul class="select-options"> <li data-value="asap">Delivery ASAP</li> <li data-value="later">Delivery Later</li> <li data-value="catering">Catering</li> </ul> <div class="modal later-modal"> <p>Select a time to deliver.</p> </div> <div class="modal catering-modal"> <p>Select a time to cater.</p> </div> 

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

相关问题 jQuery - 如何按文本选择下拉列表项? - jQuery - How to select dropdown list item by text? jQuery从下拉列表中动态选择一个项目不起作用 - Jquery dynamically select an item from dropdown not workingq 如何使用jQuery从下拉菜单中选择项目 - How to select an item from a dropdown using jQuery jQuery - 在选择下拉列表时执行 js 函数 - jQuery - Executing js function on select of dropdown list 如何从angularjs中的下拉列表中选择一个项目? - How select an item from dropdown list in angularjs? 更改还是关注jQuery? 我应该使用哪个事件与select下拉列表来调用jQuery-AJAX函数? - Change or Focus in jQuery? Which event should I use with select dropdown to give a call to jQuery-AJAX function? 如何显示从下拉列表中选择的项目不应出现在复选框列表中 - How to show item selected from dropdown should not appear in checkbox list 如何从无序列表中选择项目 - How to select item from unordered list jquery 我正在尝试从 React.js 中的另一个模态打开一个模态。 但以前的模态保持打开状态 - I am trying to open one modal from another modal in React.js. but previous modal kept open 如何打开下拉菜单 <select>与焦点jQuery事件? - How can I open a dropdown <select> with focus jquery event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM