【问题标题】:Close Modal bootstrap window after insert in database插入数据库后关闭模态引导窗口
【发布时间】:2016-10-27 08:53:58
【问题描述】:

我有模态引导窗口,它使用 AJAX 和 JQUERY 在数据库中插入数据,但插入后我无法关闭窗口,我还想显示成功插入的消息,请参见下面的代码

---- 模态窗口----

    <!-- Modal  employer-->
<div class="modal fade" id="employer" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" 
                   data-dismiss="modal">
                       <span aria-hidden="true">&times;</span>
                       <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">

                </h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">

                <form role="form" action='insert.php' method='post' id='myform'>
                  <div class="form-group">
                    <label for="exampleInputEmail1">Nom</label>
                      <input type="text"  name="nom"class="form-control"
                      id="exampleInputEmail1" placeholder="Nom"/>
                  </div>
                  <div class="form-group">
                    <label for="exampleInputPassword1">Prenom</label>
                      <input type="text" name="prenom" class="form-control"
                          id="exampleInputPassword1" placeholder="Prenom"/>
                  </div>
                                    <div class="form-group">
                    <label for="exampleInputPassword1">telephone</label>
                      <input type="text" name="telephone"class="form-control"
                          id="exampleInputPassword1" placeholder="telephone"/>
                  </div>
      <div class="form-group">
                    <label for="exampleInputPassword1">Fonction</label>
                      <input type="text" name="fonction" class="form-control"
                          id="exampleInputPassword1" placeholder="Fonction"/>
                  </div>
                      <div class="form-group">
                    <label for="exampleInputPassword1">Salire</label>
                      <input type="text" name="salaire" class="form-control"
                          id="exampleInputPassword1" placeholder="Salire"/>
                  </div>




            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">

                <button type="button" id='insert' class="btn btn-primary">
                    Save changes
                </button>
                  </form>
            </div>
        </div>
    </div>
</div>

---- insert.php ----

<?php
require('config.php');
 $nom = $_POST['nom'];
 $prenom = $_POST['prenom'];
  $telephone = $_POST['telephone'];
   $fonction = $_POST['fonction'];
    $salaire = $_POST['salaire'];
 $sql = "insert into employer (nom, prenom,telephone,fonction,salaire) values ('$nom','$prenom','$telephone','$fonction','$salaire')";

 if(mysqli_query($con, $sql)){
 echo 'success';
?>



 <script type="text/javascript">
$(document).ready(function(){

        $('#employer').modal('hide');

});
</script>
 <?php

  }
?>

---- insert.js----

 $('#myform').submit(function(){
    return false;
});

$('#insert').click(function(){
    $.post(     
        $('#myform').attr('action'),
        $('#myform :input').serializeArray(),
        function(result){
            $('#result').html(result);
        }
    );
});

【问题讨论】:

    标签: php jquery ajax twitter-bootstrap


    【解决方案1】:

    当您收到来自 ajax 请求的响应时,请关闭模式窗口。

        $('#employer').modal('hide');
    

    根据你的例子:

    $('#insert').click(function(){
        $.post(     
            $('#myform').attr('action'),
            $('#myform :input').serializeArray(),
            function(result){
                $('#employer').modal('hide'); // ADD CODE FOR CLOSE MODAL
                $('#result').html(result);
            }
        );
    

    });

    【讨论】:

    • 非常感谢你我需要的最后一件事只是在关闭前显示成功插入的消息
    • 您可以在 "$('#employer').modal('hide'); // 为关闭模式添加代码之前显示警报消息,如下所示:
    • alert('插入成功');
    • 什么模式关闭或警告?
    【解决方案2】:

    试试这个:

    $('#modal').modal('close');
    

    它将关闭模式。

    【讨论】:

    • 我已经试过了,我把它放在 insert.php 中,见上面的代码,但没有任何效果
    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2015-01-08
    • 2021-05-24
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多