收到ajax的结果后打开一个弹出框

时间:2015-10-09 04:05:52

标签: javascript php jquery ajax popup

我有一个正常运行的ajax代码并提供了所需的结果。我想修改这段代码,并希望当从ajax收到回复时,应该打开一个弹出/模态框。

我可以通过点击按钮打开弹出/模态框

<!-- Button trigger modal -->
<button class="btn btn-primary" data-toggle="modal" data-target="#bsModal3">
    Small Modal
</button>

<!-- Modal -->
<div class="modal fade" id="bsModal3" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="mySmallModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                Your content goes here...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

但不知道如何在ajax中自动打开它。

这是我的ajax代码

$.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
        txt: txtbox,
        hidden: hiddenTxt
    },
    cache: false,
    success: function (returndata) {
        if (returndata[4] === 1) {
            // want to load a popup box here
        } else {
            // other code
        }
    },
    error: function () {
        console.error('Failed to process ajax !');
    }
});

任何人都可以告诉我如何做到这一点

3 个答案:

答案 0 :(得分:4)

您可以在结果中使用$("#bsModal3").modal('show');

详细了解modal methods

&#13;
&#13;
$.ajax({
  type: 'post',
  url: 'test2.php',
  dataType: 'json',
  data: {
    txt: txtbox,
    hidden: hiddenTxt
  },
  cache: false,
  success: function(returndata) {
    if (returndata[4] === 1) {

      $("#bsModal3").modal('show');

    } else {
      // other code
    }
  },
  error: function() {
    console.error('Failed to process ajax !');
  }
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>




<!-- Modal -->
<div class="modal fade" id="bsModal3" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="mySmallModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        Your content goes here...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试将此内置成功回调:

success: function (returndata) {
    if (returndata[4] === 1) {
        $('#bsModal3').modal(); // this
    } else {
        // other code
    }
},

答案 2 :(得分:0)

请使用Ajax Call,模态是在ajax响应中调用

$.ajax({
        type: 'post',
        url: 'test2.php',
        dataType: 'json',
        data: {
            txt: txtbox,
            hidden: hiddenTxt
        },
        cache: false,
        success: function (returndata) {
            if (returndata[4] === 1) {
               $('#bsModal3').modal();  // Please right this in your Code
            } else {
                // other code
            }
        },
        error: function () {
            console.error('Failed to process ajax !');
        }
    });