用于自动加载模式的 Javascript 代码不起作用

标签 javascript php html

我使用了一些我在互联网上找到的代码,这些代码应该可以自动加载模态,但它不起作用。 这是一个实时网站,我应该对其进行一些更改。不确定这里出了什么问题:

if (isset($_SESSION['verify'])) {

    echo '
        <script type="text/javascript">
            $(document).ready(function()
            {
                $("#exampleModal").modal('show');
            }
            );
        </script>

        <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">Modaltitle</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="#" method="post">
                            <input type="text" name="otp" placeholder="Type OTP">
                            <textarea rows="1" cols="25" name="message"placeholder="Type Message"></textarea>
                            <center><button id="button2" type="submit" name="verify_otp">Send Message</button></center>
                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </div>
        </div>';
}

我希望它产生一个模式而无需按下按钮,但在自动加载时它并没有这样做。

最佳答案

    if(isset($_SESSION['verify'])){

    echo"
        <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'>Modaltitle</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='#' method='post'>
                              <input type='text' name='otp' placeholder='Type OTP'>
                              <textarea rows='1' cols='25' name='message' placeholder='Type Message'></textarea>
                              <center><button id='button2' type='submit' name='verify_otp'>Send Message</button></center>
                         </form>
                     </div>
                     <div class='modal-footer'>
                       <button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
                       <button type='button' class='btn btn-primary'>Save changes</button>
                     </div>
                 </div>
             </div>
         </div>
         <script type='text/javascript'>
           $(document).ready(function()
           {
              $('#exampleModal').modal('show');
           });
        </script>";}

我已经通过更改引号(单引号到双引号)修改了您的代码。此代码应该适合您。运行前请检查以下步骤:

  1. 您的代码中是否存在任何 JQuery 冲突?
  2. 使用浏览器控制台检查是否有任何javascript错误。

我假设您已经包含使用模态所需的 JS 和 CSS。

关于用于自动加载模式的 Javascript 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784358/

相关文章:

javascript - 如何使用 raphael js 更改 onclick 动画圆圈的大小

php - 如何返回所有行,即使它们在连接表中没有行

javascript - 隐藏和显示 div 单击 a

php - 连接到 SQL 主机问题

javascript - 该网站无法使用 loadmore 按钮

javascript - 如何拉伸(stretch) div 以适应内容直到它达到其父级的高度?

javascript - 你如何使用 JS Promises 在 API 返回值时继续从 API 请求?

Javascript (Math.pow(2,31) | 0) 产生负数

javascript - 使用 value 属性为 Select 设置默认值

php - mysql_fetch_assoc() : supplied argument is not a valid MySQL