【问题标题】:appending html to end of JQUERY AJAX call将 html 附加到 JQUERY AJAX 调用的末尾
【发布时间】:2021-04-03 15:28:42
【问题描述】:

我正在尝试做某事,经过互联网研究后我不清楚我是否可以。我想在脚本末尾添加模态的 HTML,所有这些都在 AJAX Success 调用中。这可能吗?这是我尝试过的代码:

function options() {
  jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    data: {
      action: 'test2_update_function'
    },
    success: function(data) {
      jQuery("#myModal").show(function() {
          // Get the modal
          var modal = document.getElementById("myModal");

          // Get the button that opens the modal
          var btn = document.getElementById("myBtn");

          // Get the <span> element that closes the modal
          var span = document.getElementsByClassName("close")[0];

          // When the user clicks on the button, open the modal
          btn.onclick = function() {
            modal.style.display = "block";
          }

          // When the user clicks on <span> (x), close the modal
          span.onclick = function() {
            modal.style.display = "none";
          }

          // When the user clicks anywhere outside of the modal, close it
          window.onclick = function(event) {
            if (event.target == modal) {
              modal.style.display = "none";
            }
          }
        }
        /*>>> closes the .show*/
      ).append('<div id="myModal" class="modal"><div class="modal-content"><span class="close">&times;</span><p id="my-data"></p></div></div>');
      jQuery("#my-data").html(data); //Add this line
      ;
    }
  });
}

【问题讨论】:

    标签: jquery ajax append


    【解决方案1】:

    我认为你的意思是这样的

    $(function() {
      $("body").append(`<div id="myModal" class="modal">
          <div class="modal-content"><span class="close">&times;</span>
            <p id="my-data"></p>
          </div>
        </div>`); // if you must
    
      $("#btn").on("click", function() {
        $("#modal").show()
      })
    
      $("#modal").on("click", ".close", function() {
        $("#modal").hide();
      })
    
      $(document).on("click", function(e) {
        if ($(e.target).closest("#modal").length === 0 ) {
          $("#modal").hide()
        }
      })
    })
    
    function options() {
      $.ajax({
        type: 'post',
        url: my_ajax.ajax_url,
        data: {
          action: 'test2_update_function'
        },
        success: function(data) {
          $("#my-data").html(data); //Add this line
          $("#myModal").show()
    
        }
      });
    }
    
    <div id="myModal" class="modal">
      <div class="modal-content"><span class="close">&times;</span>
        <p id="my-data"></p>
      </div>
    </div>
    

    【讨论】:

    • 有没有办法将 html 包含在函数中,使其不分离?
    • 您的附加内容没有附加到任何有用的内容。看起来您正在将带有 id="myModal" 的 div 附加到带有 id="myModal" 的东西
    • 我更新添加onload,但没必要
    • 感谢尝试将 html 保留在成功函数中,以便将其全部集中在一个位置
    • 我不推荐这个。它们并不真正相关。让演示文稿不被执行
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 2017-06-15
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多