关闭模态后,如何从模态体中删除内容?

时间:2018-05-22 23:40:52

标签: javascript jquery bootstrap-4 bootstrap-modal

我的目标是每次打开时都会显示一个新模态。目前,只有在刷新页面后首次显示模态窗口时才会出现这种情况。对于后续关闭/打开模态,它只是将当前内容附加到前一个内容的末尾,该内容仍然显示。

值得注意的是,通过尝试无数努力的这种特殊配置,我得到了一个

  

未捕获的ReferenceError:$未定义

在我的Javascript控制台中为该行

$(document).ready(function() { 

但我不确定为什么。 jQuery库(3.2.1)初始化为它所使用的脚本类型。任何线索?

<!-- Form -->
<form onsubmit="return false">

  <!-- Heading -->
  <h3 class="dark-grey-text text-center">
    <strong>Calculate your payment:</strong>
  </h3>
  <hr>

  <div class="md-form">
    <i class="fa fa-money prefix grey-text"></i>
    <input type="text" id="income" class="form-control">
    <label for="income">Your income</label>
  </div>

  <div class="text-center">
    <button type="button" class="btn btn-pink" onclick="calculator()" data-toggle="modal" data-target="#exampleModal">Calculate</button>
    <script type='text/javascript'>
      function calculator() {
        let payment = ((document.getElementById("income").value - 18210)*0.1)/12;
        payment = Math.round(payment);
        if (payment <= 0) {
          $('.modal-body').append("$0 per month.");
        }
        else {
          $('.modal-body').append(payment + " or less per month.");
        }
       }
    </script>

    <!-- Modal -->
    <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">You should be paying:</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div id="test" class="modal-body">
                </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>

    <!-- Clear content from modal -->
    <script type="text/javascript">
      $(document).ready(function() {
        $('.exampleModal').on('hidden.bs.modal', function () {
          $('.modal-body').clear();
        });
      });
    </script>
    <hr>
    <label class="grey-text">* This is just an estimate. Please call for a quote.</label>
    </fieldset>
  </div>

</form>
<!-- Form -->

2 个答案:

答案 0 :(得分:0)

尝试$('modal-body').empty()代替clear

答案 1 :(得分:0)

您应该使用.html.text代替.append

if (payment <= 0) {
  $('.modal-body').text("$0 per month.");
}
else {
  $('.modal-body').text(payment + " or less per month.");
}

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Form -->
            <form onsubmit="return false">

              <!-- Heading -->
              <h3 class="dark-grey-text text-center">
                <strong>Calculate your payment:</strong>
              </h3>
              <hr>

              <div class="md-form">
                <i class="fa fa-money prefix grey-text"></i>
                <input type="text" id="income" class="form-control">
                <label for="income">Your income</label>
              </div>

              <div class="text-center">
                <button type="button" class="btn btn-pink" onclick="calculator()" data-toggle="modal" data-target="#exampleModal">Calculate</button>
                <script type='text/javascript'>
                  function calculator() {
                    let payment = ((document.getElementById("income").value - 18210)*0.1)/12;
                    payment = Math.round(payment);
                    if (payment <= 0) {
                      $('.modal-body').text("$0 per month.");
                    }
                    else {
                      $('.modal-body').text(payment + " or less per month.");
                    }
                  }
                </script>

                <!-- Modal -->
                <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">You should be paying:</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div id="test" class="modal-body">
                            </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>

                <!-- Clear content from modal -->
                <script type="text/javascript">
                  $(document).ready(function() {
                    $('.exampleModal').on('hidden.bs.modal', function () {
                      $('.modal-body').clear();
                    });
                  });
                </script>
                <hr>
                <label class="grey-text">* This is just an estimate. Please call for a quote.</label>
                </fieldset>
              </div>

            </form>
            <!-- Form -->
&#13;
&#13;
&#13;