[英]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.