在ajax调用后阻止页面刷新

时间:2016-11-01 15:05:52

标签: javascript jquery ajax twitter-bootstrap

我有一个bootstrap格式的信息,我使用ajax调用将其保存到json文件中。

我的问题是,当我按下提交按钮时,页面似乎在通话后刷新(在点击提交按钮后执行),这是我一定要避免的。

我试图调查这个问题,但我的知识不足以解决这个问题,因为我对这些概念都没有深刻的理解。

这是我的BS表格:

<form class="form-horizontal">
      <div class="form-group">
        <label class="control-label col-sm-2" for="name">Full Name:</label>
        <div class="col-sm-2">
          <input type="name" class="form-control" id="nameFull">
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2" for="phone">Phone:</label>
        <div class="col-sm-2">
          <input type="phone" class="form-control" id="phoneApp">
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2" for="email">Email:</label>
        <div class="col-sm-2">
          <input type="email" class="form-control" id="emailApp">
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2" for="date">Date:</label>
        <div class="col-sm-2">
          <input type="date" class="form-control" id="date">

        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2" for="text">Hour:</label>
        <div class="col-sm-2">
          <select class="form-control" id="time">
            <option value="Time" class="">Time</option>
                        <option value="10am" class="">10:00-10:30</option>
                        <option value="1030am" class="">10:30-11:00</option>
                        </select>
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-lg-2" for="reason">Reason:</label>
        <div class="col-lg-3">
          <textarea class="form-control" id="reason" name="comments" placeholder="Describe the reason to make an appointment here. Please, include symptoms and any historical data that may help us determine your case." rows="5"></textarea><br>
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button id="saveAppt" class="btn btn-default">Submit</button>
        </div>
      </div>
    </form>

这是ajax电话:

$(document).ready(function() {
    $("#saveAppt").click(function(){
         var fullName = $("#nameFull").val();
         var userName = $("#cpr").val();
         var phone = $("#phoneApp").val();
         var email = $("#emailApp").val();
         var date = $("#date").val();
         var time = $("#time").val();
         var reason = $("#reason").val();
         console.log(time);
         console.log(date);
         console.log(reason);

         var jAppointment= {};
        jAppointment.fullName = fullName;
        jAppointment.userName = userName;
        jAppointment.phone = phone;
        jAppointment.email = email;
        jAppointment.date = date;
        jAppointment.time = time;
        jAppointment.reason = reason;

        console.log(JSON.stringify(jAppointment));

      $.ajax
      ({
        type: "GET",
        dataType : 'json',
        async: false,
        url: 'save-appointments.php',
        data: { data: JSON.stringify(jAppointment) },
        success: function () {console.log("Thanks!"); },
        failure: function() {console.log("Error!");}
      });
    });
});

2 个答案:

答案 0 :(得分:7)

现在,提交操作会刷新您的网页,因为<button>代码type='submit'默认只需添加type='button'类型以避免提交/刷新,因此永远不会引发点击事件:

<button type="button" id="saveAppt" class="btn btn-default">Submit</button>

希望这有帮助。

$(document).ready(function() {
  $("#saveAppt").click(function(){
    var fullName = $("#nameFull").val();
    var userName = $("#cpr").val();
    var phone = $("#phoneApp").val();
    var email = $("#emailApp").val();
    var date = $("#date").val();
    var time = $("#time").val();
    var reason = $("#reason").val();
    
    console.log(time);
    console.log(date);
    console.log(reason);

    var jAppointment= {};
    jAppointment.fullName = fullName;
    jAppointment.userName = userName;
    jAppointment.phone = phone;
    jAppointment.email = email;
    jAppointment.date = date;
    jAppointment.time = time;
    jAppointment.reason = reason;

    console.log(JSON.stringify(jAppointment));

    $.ajax
    ({
      type: "GET",
      dataType : 'json',
      async: false,
      url: 'save-appointments.php',
      data: { data: JSON.stringify(jAppointment) },
      success: function () {console.log("Thanks!"); },
      failure: function() {console.log("Error!");}
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal">
  <div class="form-group">
    <label class="control-label col-sm-2" for="name">Full Name:</label>
    <div class="col-sm-2">
      <input type="name" class="form-control" id="nameFull">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="phone">Phone:</label>
    <div class="col-sm-2">
      <input type="phone" class="form-control" id="phoneApp">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="email">Email:</label>
    <div class="col-sm-2">
      <input type="email" class="form-control" id="emailApp">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="date">Date:</label>
    <div class="col-sm-2">
      <input type="date" class="form-control" id="date">

    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="text">Hour:</label>
    <div class="col-sm-2">
      <select class="form-control" id="time">
        <option value="Time" class="">Time</option>
        <option value="10am" class="">10:00-10:30</option>
        <option value="1030am" class="">10:30-11:00</option>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-lg-2" for="reason">Reason:</label>
    <div class="col-lg-3">
      <textarea class="form-control" id="reason" name="comments" placeholder="Describe the reason to make an appointment here. Please, include symptoms and any historical data that may help us determine your case." rows="5"></textarea><br>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type='button' id="saveAppt" class="btn btn-default">Submit</button>
    </div>
  </div>
</form>
<br><br><br><br><br><br>

答案 1 :(得分:1)

在点击处理程序中创建对您的事件的引用。

$("#saveAppt").click(function(e){

然后在AJAX调用之后调用该事件的preventDefault函数。

     $.ajax
      ({
        type: "GET",
        dataType : 'json',
        async: false,
        url: 'save-appointments.php',
        data: { data: JSON.stringify(jAppointment) },
        success: function () {console.log("Thanks!"); },
        failure: function() {console.log("Error!");}
      });
      e.preventDefault();