【问题标题】:How to reset bootstrap modal to its original contents on close? [duplicate]如何在关闭时将引导模式重置为其原始内容? [复制]
【发布时间】:2016-01-06 04:39:47
【问题描述】:

P.S 我的 javascript 不是很好,我知道有人问过这个问题,但解决方案不适用于我的问题,所以我会再次问这个问题。

我被这个问题困住了,还没有找到解决方案。我想清除模态关闭后发生的所有事情。

我的模态是一个表单模态,所以每当我点击提交时,codeigniter 验证消息将通过在 <div style="font-size:10px;width:50%;" id="info"></div> 上使用 ajax 来显示。现在,每当我关闭模式时,验证消息都会在我重新打开时保留。关闭模态后应该怎么做才能清除这些?

这是ajax调用的函数:

public function addcomp () {
  $this->load->library('form_validation');
  $this->load->helper('security');
  $this->form_validation->set_rules('comp_name','Computer Name','trim|required|callback_pc_exist');
  $this->form_validation->set_rules('status','Status','trim|required');
  $this->form_validation->set_rules('location','Location','trim|required');

  if ($this->form_validation->run()) {

    $this->load->model('asset_model');
    $result=$this->asset_model->addpc();

    if (!$result) {
      echo mysqli_error($result);
    } else {
      echo "<div class='alert alert-success alert-dismissable'>
      <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
      Computer Successfully Added!</div>";
    }
  }
  echo validation_errors("<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>","</div>");
}

这是提交调用的ajax:

$(document).ready(function(){
  $("#addpc").submit(function(){
    var formdata=$("#addpc").serialize();
    $.ajax({
      url:"asset_management/addcomp",
      type:"POST",
      data: formdata,
      async: true,
    }).done(function( formdata ) {
      $('#info').html(formdata);
      $("#addpc")[0].reset();
      viewdata();
    });
    return false;
  });
});

【问题讨论】:

  • 如何添加验证消息?有具体的吗?
  • @PraveenKumar 编辑了我的帖子
  • 是的,已检查。请检查我的答案。将 #info 替换为 .alert-error 或任何需要删除的类。
  • 更新的答案对你有帮助吗,哥们?

标签: javascript jquery twitter-bootstrap bootstrap-modal


【解决方案1】:

您可以使用Bootstrap's Modal Events。你应该关心这两个:

  • hide.bs.modal 当 hide 实例方法被调用时立即触发该事件。
  • hidden.bs.modal 当模式对用户完成隐藏时触发此事件(将等待 CSS 转换完成)​​。

所以,你可以这样做:

$(function () {
  // Delegating to `document` just in case.
  $(document).on("hidden.bs.modal", "#myModalID", function () {
    $(this).find("#info").html(""); // Just clear the contents.
    $(this).find("#info").remove(); // Remove from DOM.
  });
});

更新

看到你的代码,你可能需要使用这个:

$(function () {
  // Delegating to `document` just in case.
  $(document).on("hidden.bs.modal", "#myModalID", function () {
    $(this).find(".alert-danger").remove(); // Remove from DOM.
  });
});

【讨论】:

  • @GuruprasadRao 哈哈哈。请参阅 OP 的帖子:$("#addpc")[0].reset();.
  • 对不起 Praveen.. 我以为他会在每个字段上都进行必需的字段验证,并且他想删除。那么重置表单会是更好的选择吗?
  • @GuruprasadRao 这看起来像是服务器端的硬编码验证。
  • 如果我回答的话,我现在会失去声誉..:P
  • 是的..刚刚读到他也将所有这些都放在一个div中..您的解决方案应该可以工作... :)
猜你喜欢
  • 1970-01-01
  • 2020-07-11
  • 2015-10-17
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
相关资源
最近更新 更多