提交后模态未隐藏

时间:2018-08-05 23:26:22

标签: javascript jquery twitter-bootstrap modal-dialog

我一直在尝试使函数能够使用模态发布评论。 JavaScript负责发布评论,但是'hide'选项不起作用。 我已经在这个问题上待了好几个小时,但我不知道自己在做什么错。 任何帮助都将受到欢迎。谢谢!

function postcomment(id) {
  $("#postcommentid").val(id);
  $('#postComments').modal('show');
  return false;
}

$('#frmpostcomments').submit(function() {
  var date = new Date();
  yr = date.getFullYear(), month = +date.getMonth() + 1 < 10 ? '0' + parseInt(date.getMonth() + 1) : date.getMonth() + 1,

    day = +date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
    newDate = yr + '-' + month + '-' + day + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
  $.ajax({
    url: '<?php echo base_url("comments/create");?>',
    type: "POST",
    data: {
      post_id: $("#postcommentid").val(),
      comment: $("textarea#comment").val(),
      created_on: newDate,
    },
    dataType: "text",
    cache: false,
    success: function(data) {
      if (data == 'Created') {

        alert("comment has been posted.");
        $('#postComments').modal('hide');
      }
    }
  });
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="modal inmodal" id="postComments" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content animated fadeIn">
      <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">Post a Comment</h4>
        <div class="row">

        </div>
      </div>
      <div class="modal-body">
        <form role="form" id="frmpostcomments" action="<?php echo base_url(" comments/create ");?>" method="post">

          <div class="form-group">
            <label>Comment</label>
            <input type="hidden" name="postcommentid" id="postcommentid" value="" />

            <textarea name="comment" required id="comment" cols="30" rows="5" class="form-control"></textarea>
          </div>
          <div class="form-group">
            <button class="btn btn-primary" type="submit">Post Comment</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

0 个答案:

没有答案