延迟后如何显示模态?

时间:2018-12-19 10:37:38

标签: javascript ajax html5 servlets bootstrap-modal

我希望如果用户在我的网站上停留约20秒钟,则会显示一个询问用户名和电子邮件地址的模式。我已经获取了模态div的ID,并将其存储在函数myFunction()中。这里的主要问题是20秒后什么也没显示。

以下是我的代码:-

<!-- Apart from window.onload, I have also mentioned the function with body onload-->

<body onload="myFunction" id="myPage" data-spy="scroll" data-target=".navbar" data-offset="60">

            <script type="text/javascript">

            window.setTimeout(function myFunction() {

                document.getElementById("exampleModal");

                },20000);

            </script>

        <!-- Modal code -->

             <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
             <div class="modal-dialog" role="document">
             <div class="modal-content">
             <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">New message</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <form action="MessageServlet" method="post">
                      <div class="form-group">
                        <label for="recipient-name" class="col-form-label">Recipient:</label>
                        <input type="text" class="form-control" id="recipient-name" value="svchaturvedi9@gmail.com" readonly="readonly">
                      </div>
                      <div class="form-group">
                        <label for="recipient-name" class="col-form-label">Your Email ID:</label>
                        <input type="text" class="form-control" id="email-name" name="email-name" placeholder="Email ID">
                      </div>
                      <div class="form-group">
                        <label for="message-text" class="col-form-label">Message:</label>
                        <textarea class="form-control" id="message-text" name="message-text"></textarea>
                      </div>


                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Send message</button>
                    </form>
                   </div> 
                  </div>

                </div>

              </div>

我的目的是,当用户在模式字段中输入一个值并单击“确定”时,用户的数据将被发送到电子邮件servlet。 谢谢

1 个答案:

答案 0 :(得分:1)

您可以看到以下示例。根据需要替换第二和模式代码。

setTimeout(function() {
    $('#myModal').modal('toggle');
}, 200);
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>