简体   繁体   English

ajax成功响应后如何刷新div

[英]How to refresh div after ajax success response

I have a form with ajax requests, when the fields are not respected and click on submit button the messages are displayed without page refresh.我有一个带有 ajax 请求的表单,当不尊重字段并单击提交按钮时,将显示消息而不刷新页面。 This works, the problem is that if the form is submitted several times, the error messages accumulate and multiple messages are displayed at the same time.这行得通,问题是如果多次提交表单,错误消息会累积并同时显示多条消息。

For example: if you leave field name blank and then submit, the error message (field required) appears.例如:如果您将字段名称留空然后提交,则会出现错误消息(必填字段)。 When you enter your name and then submit the success message (Settings Saved) appears but the error message is still visible.当您输入您的姓名,然后提交成功消息(设置已保存)出现但错误消息仍然可见。

What I want to get is only one message at a time, so the div should update showing the new message and deleting the previous one.我想一次只得到一条消息,因此 div 应该更新显示新消息并删除前一条消息。

Now, I've tried doing some research and I understand that an if condition with $("#my-div").load("#my-div");现在,我尝试进行一些研究,并且我理解$("#my-div").load("#my-div");的 if 条件。 can be used in these cases but i have no idea how to do it, i am new to all this.可以在这些情况下使用,但我不知道该怎么做,我对这一切都很陌生。

Can anyone help me understand how to achieve this?谁能帮助我了解如何实现这一目标? I appreciate any help and thank you for any replies.感谢您的任何帮助,并感谢您的任何回复。

My form我的表格

<form name="Form" class="mts-edit-account" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post" enctype="multipart/form-data" <?php add_action( 'woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag' );?> > 
  
  <!-- Message section -->
  <div class="msg_box"></div>

  <!-- Fist & Last Name Field -->
  <div class="row name_surname">
    <div class="form-row">
      <label class="t3" for="account_first_name">Nome *</label>
      <input type="text" placeholder="Inserisci il tuo nome" class="field-settings" name="account_first_name" id="account_first_name" value="<?php echo esc_attr( $user->first_name ); ?>" />
    </div>

    <div class="form-row">
      <label class="t3" for="account_last_name">Cognome *</label>
      <input type="text" placeholder="Inserisci il tuo cognome" class="field-settings" name="account_last_name" id="account_last_name" value="<?php echo esc_attr( $user->last_name ); ?>" />
    </div> 

    <!-- Save Settings -->
    <p style="margin-bottom: 0px!important;">
      <?php wp_nonce_field( 'save_account_details', 'save-account-details-nonce' ); ?>
      <button type="submit" class="edit-account-button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Salva modifiche', 'woocommerce' ); ?></button>
      <input type="hidden" name="action" value="save_account_details" />
    </p>
  </div>
</form>

Script脚本

jQuery(document).ready(function($) {
    
    $('.mts-edit-account').on('submit', function(e) { 
        e.preventDefault(); 

    //Ajax function
    jQuery.ajax({
      
      type: "post",
      data: jQuery(".mts-edit-account").serialize(),
      url: "wp-admin/admin-ajax.php",
      success : function( response ) {
        jQuery('.msg_box').append(response);  
      }

    });
    });
});

Functions.php功能.php

add_action( 'wp_ajax_save_account_details', 'save_account_details' );
add_action( 'woocommerce_save_account_details_errors','save_account_details', 10, 1 );
function save_account_details() {

  if (trim($_POST['account_first_name']) == '') {
    $response = wc_print_notices( $return = false );
  } else {
    $response = "Settings Saved!";
  }

  // Don't forget to exit at the end of processing
  echo json_encode($response);
  exit();

}

You've used jQuery('.msg_box').append(response) method for displaying the messages.您已经使用jQuery('.msg_box').append(response)方法来显示消息。 The documentation clearly states that this is used to insert content, specified by the parameter, to the end of each element in the set of matched elements.文档明确指出,这用于将由参数指定的内容插入到匹配元素集中每个元素的末尾。

Naturally giving you the multiple messages.自然地给你多条消息。 If you just need a single message, then use:如果您只需要一条消息,请使用:

jQuery('.msg_box').html(response)

.html() will ensure that the content of the container is overridden each time. .html()将确保每次都覆盖容器的内容。

Reference: Official Documentation参考:官方文档

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

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