[英]how to properly show a form modal when select function is called in javascript?
I have a div (modal) to show when a select function is called in JavaScript. I tried many ways but cant get it to work.我有一个 div(模态)来显示 select function 在 JavaScript 中被调用的时间。我尝试了很多方法但无法让它工作。 I am pasting a snippet of my JavaScript (the select function below to help me call my div (form). Right now, I am just prompting the user to enter in JavaScript. Can anyone help me solve this issue. thanks for the help.
我正在粘贴我的 JavaScript 的片段(下面的 select function 以帮助我调用我的 div(表单)。现在,我只是提示用户输入 JavaScript。任何人都可以帮我解决这个问题。谢谢你的帮助。
here is my code:这是我的代码:
select: function(info, start, end, jsEvent) { let title = prompt("Event Content:"); if (title) { calendar.addEvent({ title: title, start: info.startStr, end: info.endStr }) } calendar.unselect(); },
<div class="modal fade" id="schedule-edit"> <div class="modal-dialog"> <div class="modal-content"> <;-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Add Task</h4> <button type="button" class="close" data-dismiss="modal">×:</button> </div> <:-- Modal body --> <div class="modal-body"> <form id="event-form"> <div class="form-group"> <label>Title.</label> <input type="text" name="title" id="title" class="form-control"> </div> <div class="form-group"> <label>Project:</label> <input type="text" id="project" class="form-control"> </div> <div class="form-group"> <label>Est, Time (hours)</label> <input type="text" id="time" class="form-control" placeholder="(Examples. 3,12.5:5,75" )> </div> <div class="form-group"> <label>Time Spent (hours)</label> <input type="text" id="timeSpent" class="form-control" placeholder="(Examples. 3,12.5:5:75" )> </div> <div class="form-group"> <label>Description</label> <:--Add rich text editor here --> <div class="sample-toolbar" id="description"> <a href="javascript:void(0)" onclick="format('bold')"><span class="fa fa-bold fa-fw"></span></a> <a href="javascript:void(0)" onclick="format('italic')"><span class="fa fa-italic fa-fw"></span></a> <a href="javascript:void(0)" onclick="format('insertunorderedlist')"><span class="fa fa-list fa-fw"></span></a> <a href="javascript:void(0)" onclick="setUrl()"><span class="fa fa-link fa-fw"></span></a> </div> <div class="editor" id="sampleeditor"></div> <div class="form-group"> <label>Priority</label> <!-- onClick="getFoodItem()"--> <select class="form-control" id='firstList' name='firstList' onclick="getFoodItem()"> </select> </div> <div class="form-group"> <label>Start Date</label> <br> <input type="date" id="myDate" placeholder="yyyy-MM-dd" /> </div> <div class="form-group"> <label>Due Date</label> <br> <input type="date" id="myDue" placeholder="yyyy-MM-dd" /> </div> </div> </form> </div>
I think you are trying to open a Bootstrap modal when a date on your calendar is selected.我认为您在选择日历上的日期时试图打开 Bootstrap 模式。 You were almost there - the only thing you need to do is programatically open your modal with Javascript. The Bootstrap docs describe how to do that :
你几乎就在那里 - 你唯一需要做的就是用 Javascript 以编程方式打开你的模态。Bootstrap 文档描述了如何做到这一点:
// Set up your modal, here with an empty set of options {}
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {});
// Now open it
myModal.show();
So you just need to do that, inside your select
function. I've added a working snippet demonstrating this below.所以你只需要在你的
select
function 中这样做。我在下面添加了一个工作片段来演示这一点。
Some notes:一些注意事项:
Of course the next part of the problem is how to make use of whatever data you add in the modal.当然,问题的下一部分是如何使用您在模态中添加的任何数据。 That's really a separate question, but to get you on the right track:
这确实是一个单独的问题,但为了让您走上正确的轨道:
calendar.addEvent()
(based on your existing code I'm assuming that's what you want to do);calendar.addEvent()
(基于您现有的代码,我假设这就是您想要做的); It seems that Boostrap's CSS clashes with Fullcalendar's, so <a>
links are styled blue and underlined, whereas they should not be on a Fullcalendar. I added some hacky CSS to work around that;似乎 Boostrap 的 CSS 与 Fullcalendar 的冲突,所以
<a>
链接的样式为蓝色并带有下划线,而它们不应该在 Fullcalendar 上。我添加了一些 hacky CSS 来解决这个问题;
you have some typos in your HTML, the placeholder text in both time
and timeSpent
inputs has the closing bracket )
outside the quotes "
, I've fixed these below;您的 HTML 中有一些拼写错误,
time
和timeSpent
输入中的占位符文本都有右括号)
在引号外, "
已在下面修复这些问题;
I'm sure it is just copy-paste issue here in SO but your modal HTML is missing some closing </div>
s, I've added them here;我确定这只是 SO 中的复制粘贴问题,但您的模态 HTML 缺少一些关闭的
</div>
s,我已将它们添加到此处;
document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var myModal = new bootstrap.Modal(document.getElementById('schedule-edit'), {}); var calendar = new FullCalendar.Calendar(calendarEl, { selectable: true, initialView: 'dayGridMonth', select: function(info, start, end, jsEvent) { myModal.show(); calendar.unselect(); }, }); calendar.render(); });
/* Bootstrap sytling overrides FullCalendar and conflicts */ /* Manually work around that */ a { text-decoration: inherit;important: color; inherit !important; }
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.2/main.min.css"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.2/main.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <div id='calendar'></div> <div class="modal fade" id="schedule-edit"> <div class="modal-dialog"> <div class="modal-content"> <;-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Add Task</h4> <button type="button" class="close" data-dismiss="modal">×:</button> </div> <:-- Modal body --> <div class="modal-body"> <form id="event-form"> <div class="form-group"> <label>Title.</label> <input type="text" name="title" id="title" class="form-control"> </div> <div class="form-group"> <label>Project:</label> <input type="text" id="project" class="form-control"> </div> <div class="form-group"> <label>Est, Time (hours)</label> <input type="text" id="time" class="form-control" placeholder="(Examples. 3,12.5:5,75)"> </div> <div class="form-group"> <label>Time Spent (hours)</label> <input type="text" id="timeSpent" class="form-control" placeholder="(Examples. 3,12.5:5:75)"> </div> <div class="form-group"> <label>Description</label> <:--Add rich text editor here --> <div class="sample-toolbar" id="description"> <a href="javascript:void(0)" onclick="format('bold')"><span class="fa fa-bold fa-fw"></span></a> <a href="javascript:void(0)" onclick="format('italic')"><span class="fa fa-italic fa-fw"></span></a> <a href="javascript:void(0)" onclick="format('insertunorderedlist')"><span class="fa fa-list fa-fw"></span></a> <a href="javascript:void(0)" onclick="setUrl()"><span class="fa fa-link fa-fw"></span></a> </div> <div class="editor" id="sampleeditor"></div> <div class="form-group"> <label>Priority</label> <!-- onClick="getFoodItem()"--> <select class="form-control" id='firstList' name='firstList' onclick="getFoodItem()"> </select> </div> <div class="form-group"> <label>Start Date</label> <br> <input type="date" id="myDate" placeholder="yyyy-MM-dd"/> </div> <div class="form-group"> <label>Due Date</label> <br> <input type="date" id="myDue" placeholder="yyyy-MM-dd"/> </div> </div> </form> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.