javascript - 为什么我的模态窗口在点击后没有关闭?

标签 javascript html css modal-dialog

我的 HTML 中有两个元素。在每个标签中都有一个问号图标。单击问号会触发一个模式窗口。这是我的 html 代码看起来像

<form>
<div class="form-group">
            <label class="control-label col-md-4 col-lg-3 required" for="offer_title" id="ThemaLbl">
                <div class="inline-help form-label"><i onClick="myFunction('themaModal')" class="fa fa-question-circle-o" id="ThemaHilfe"></i></div>Thema</label>
            <div class="col-md-8 col-lg-9">
                <textarea class="longInput" cols="42" rows="1" id="ThemaTextfield"></textarea>
            </div>
</div>

<!-- The Modal -->
        <div id="themaModal" class="modal">

            <!-- Modal content -->
            <div class="modal-content">
                <div class="modalheader">
                    <h2><p><b id="hilfeTitel">Wie kann ich eine neue Anzeige erstellen?</b></p></h2>
                </div>
                <div>
                    <p style="display: block;"><font size="6" id="hilfeText"><b id="hilfeText">Sie wollen eine neue Anzeige anlegen?</b> Dann tragen Sie die zugehörigen Informationen ein und speichern
        das Ergebnis am Ende ab.</font></p>
                </div>
                <div>
                    <div class="panel panel-default" id="feedback-form">
                        <div class="panel-heading">
                            <h4 class="panel-title" id="hilfeQuestion">
        <i class="fa fa-question-circle"></i> Waren die Informationen für Sie hilfreich? </h4>
                        </div>
                        <div class="panel-body">
                            <form class="feedback" id="new_feedback" role="form" action="/feedbacks" method="post" accept-charset="UTF-8" data-remote="true">
                                <input name="utf8" type="hidden" value="✓">
                                <div class="form-group">
                                    <textarea name="feedback[comment]" class="form-control" id="feedback_comment" placeholder="Sie können uns hier Ihre Anmerkungen mitteilen."></textarea>
                                </div>
                                <input name="feedback[context]" id="feedback_context" type="hidden" value="/offers/new">
                                <input name="feedback[help_key]" id="feedback_help_key" type="hidden" value="help.offers.new">
                            </form>
                            <div class="hidden response">
                                Vielen Dank für Ihr Feedback!
                            </div>
                        </div>
                    </div>

                    <div class="sa-button-container">
                        <button tabindex="2" class="cancel" style="display: none; box-shadow: none;">Cancel</button>
                        <div class="sa-confirm-button-container">
                            <button id="commentOK" tabindex="1" class="confirm" style="display: inline-block; box-shadow: 0px 0px 2px rgba(140,212,245,0.8), inset 0px 0px 0px 1px rgba(0,0,0,0.05);">OK</button>
                            <div class="la-ball-fall">
                                <div></div>
                                <div></div>
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

<div class="form-group">
            <label class="control-label col-md-4 col-lg-3" for="offer_content" id="InhaltLbl">
                <div class="inline-help form-label" ><i onClick="myFunction('inhaltModal')" class="fa fa-question-circle-o" id="InhaltHilfe"></i></div>Inhalt</label>
            <div class="col-md-8 col-lg-9">
                <textarea class="longInput" cols="42" rows="4" id="InhaltTextfield"></textarea>
            </div>
        </div>

        <!-- The Modal -->
        <div id="inhaltModal" class="modal">

            <!-- Modal content -->
            <div class="modal-content">
                <div class="modalheader">
                    <h2><p><b id="hilfeTitel">Wie kann ich eine neue Anzeige erstellen?</b></p></h2>
                </div>
                <div>
                    <p style="display: block;"><font size="6" id="hilfeText"><b id="hilfeText">Sie wollen eine neue Anzeige anlegen?</b> Dann tragen Sie die zugehörigen Informationen ein und speichern
        das Ergebnis am Ende ab.</font></p>
                </div>
                <div>
                    <div class="panel panel-default" id="feedback-form">
                        <div class="panel-heading">
                            <h4 class="panel-title" id="hilfeQuestion">
        <i class="fa fa-question-circle"></i> Waren die Informationen für Sie hilfreich? </h4>
                        </div>
                        <div class="panel-body">
                            <form class="feedback" id="new_feedback" role="form" action="/feedbacks" method="post" accept-charset="UTF-8" data-remote="true">
                                <input name="utf8" type="hidden" value="✓">
                                <div class="form-group">
                                    <textarea name="feedback[comment]" class="form-control" id="feedback_comment" placeholder="Sie können uns hier Ihre Anmerkungen mitteilen."></textarea>
                                </div>
                                <input name="feedback[context]" id="feedback_context" type="hidden" value="/offers/new">
                                <input name="feedback[help_key]" id="feedback_help_key" type="hidden" value="help.offers.new">
                            </form>
                            <div class="hidden response">
                                Vielen Dank für Ihr Feedback!
                            </div>
                        </div>
                    </div>

                    <div class="sa-button-container">
                        <button tabindex="2" class="cancel" style="display: none; box-shadow: none;">Cancel</button>
                        <div class="sa-confirm-button-container">
                            <button id="commentOK" tabindex="1" class="confirm" style="display: inline-block; box-shadow: 0px 0px 2px rgba(140,212,245,0.8), inset 0px 0px 0px 1px rgba(0,0,0,0.05);">OK</button>
                            <div class="la-ball-fall">
                                <div></div>
                                <div></div>
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</form>

第一个盒子工作正常。但是,由于某种原因,第二个框在单击“确定”按钮后并未关闭。我的 .css 看起来像这样:

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 999; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}


.modalheader{

    margin: -17px -17px 15px;
    padding: 10px 15px;
    background-color: #f2fcde;
    border-bottom: 1px solid silver;
    font-weight: 600;
    line-height: 40px;
    display: -ms-flexbox !important;
    font-family: inherit;
    font-size: 7;
}

这是我的js:

function myFunction(modalElem){
// Get the modal
var modal = document.getElementById(modalElem);

// Get the <span> element that closes the modal
var span = document.getElementById("commentOK");

  modal.style.display = "block";


// When the user clicks on <span> (OK), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

我尝试将模态窗口的代码放在 html 文件底部靠近正文的“表单”之外,但这没有帮助。我不是经验丰富的 html 程序员,无论错误是什么,我都找不到。我该如何解决这个问题?

最佳答案

var modal = document.getElementById(ok);

我认为问题是 getElementById 里面的 ok 必须是一个字符串,试试:

var modal = document.getElementById('ok');

关于javascript - 为什么我的模态窗口在点击后没有关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411366/

相关文章:

html - 试图用 img 替换 fa awesome 图标

html - 为什么 &lt;input&gt; 中的文本放置方式与 <span> 或 &lt;textarea&gt; 中的不同?

javascript - 无法使用函数用值填充 Javascript 对象属性

javascript - 为什么我的表单发布函数会打开我的 .pl 文件而不是执行它?

javascript - 如何将css和js文件包含到php模板中?

javascript - 如何显示/隐藏生成的无序列表

javascript - 到底如何为 jQuery 的触发器传递数据?

javascript - 坚持均匀调整图像动画大小

javascript - += 有效但是++

javascript - 在 Canvas 上绘制多个正方形