取消好友确认对话框

问题描述 投票:0回答:0

我正在开发 BuddyPress 主题(社交网络)/WordPress 平台。在删除朋友之前,我需要为用户显示一个确认对话框。关于我的代码,当我在确认对话框中单击“取消”按钮或“确定”按钮时,结果是删除了朋友。

我想让代码在点击“取消”按钮时撤销好友删除,只有在点击“确定”按钮时才删除好友。

    function my_custom_js() {
    ?>
    <script type="text/javascript">
      jQuery(document).ready(function($) {
        // Listen for click events on friendship remove buttons
        $(document).on('click', '.friendship-button.is_friend.remove', function(event) {
          event.preventDefault();
          var button = $(this);
          var url = button.data('bp-nonce');

          // Display a confirmation dialog before removing the friend
          if (confirm("Are you sure you want to remove this friend?")) {
            // If the user confirms, send the AJAX request to remove the friend
            $.ajax({
              type: 'POST',
              url: url,
              dataType: 'json',
              success: function(response) {
                // If the request is successful, remove the friend from the DOM
                button.closest('li').remove();
              },
              error: function(xhr, status, error) {
                console.error(status + ': ' + error);
              }
            });
          } else {
            // If the user cancels, do nothing
            console.log('Friend removal canceled.');
          }
        });
      });
    </script>
    <?php
}
add_action( 'wp_head', 'my_custom_js' );

php jquery wordpress social-networking buddypress
© www.soinside.com 2019 - 2024. All rights reserved.