简体   繁体   English

每次单击提交按钮时,如何执行新的AJAX?

[英]How do you execute a new AJAX everytime a submit button is clicked?

I have a form that is executed every time a submit button is clicked. 我有一个表单,每次单击提交按钮时都会执行。 When the submit button is clicked, a modal is shown and the modal is populated with JSON data. 单击提交按钮后,将显示一个模式,并使用JSON数据填充该模式。 The application /addresschecker checks against the addresses posted and sends me an error message if I get a code return number of 2003. If not I select the return data via JSON using jQuery's $.each 应用程序/ addresschecker会检查发布的地址,如果返回的代码返回值为2003,则会向我发送错误消息。否则,我将使用jQuery的$ .each通过JSON选择返回数据。

The application works but when I close the modal, refill out the form and click submit, the form does not make a new call to /addresschecker I looked in my network tab of chrome and it seems to be using the old data. 该应用程序可以运行,但是当我关闭模式,重新填写表格并单击提交时,该表格不会对我在chrome的网络标签中查看的/ addresschecker进行新调用,并且似乎正在使用旧数据。 I am thinking that I need to force a new Ajax call everytime a user clicks on the submit button or clear the cache somehow. 我认为,每次用户单击“提交”按钮或以某种方式清除缓存时,都需要强制执行一个新的Ajax调用。 Not sure why I'm seeing old data 不知道为什么我看到旧数据

<form id="Validate">
  <input class="form-control" id="adr1" name="address1" type="text" placeholder="Address 1" />
  <input class="form-control" id="adr2" name="address1" type="text" placeholder="Address 1" />
  <button type="submit" >Submit</button>
</form>

<div class="modal hide">
  <!-- JSON Data returned -->
  <div id="Message_1"></div>
  <div id="Message_2"></div>
  <div id="error_message"></div>
</div>


// My main form code
submitHandler: function(form) {
  $.ajax({
    url: '/addresschecker',
    type: 'post',
    cache: false,
    dataType: 'json',
    data: $('form#Validate').serialize(),
    success: handleData
  });

  function handleData(data) {
    var mesgcheck = data.message;

    if (data.code == '2003') {
      $("#error_messag").html(mesgcheck);
    } else {
      // Display Modal
      $(".modal").removeClass("hide");
      $.each(data, function(i, suggest) {
        $(".adr1").val(suggest.address1);
        $(".adr2").val(suggest.address2);
      });                        
    }
  }
}

Let ajax handle your request. 让ajax处理您的请求。

Use this: 用这个:

<input type="button" value="submit">

Instead of type submit. 而不是类型提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM