【问题标题】:location.href displaying the whole pagelocation.href 显示整个页面
【发布时间】:2021-06-21 01:24:14
【问题描述】:

我正在将数据插入数据库并在页面上显示数据而不刷新页面。

我尝试了$("#e-empListwrap").load(location.href + "#e-empListwrap");,但它在我的页面上显示了整个页面。

$("form[name='emp']").validate({
  rules: {
    //rules
  },

  submitHandler: function(form) {

    var form = $('form[name="emp"]').get(0);

    var formData = new FormData(form);
    $.ajax({
      url: baseUrl + "/controller/emp_control.php",
      type: "POST",
      dataType: 'json',
      data: formData,
      contentType: false,
      cache: false,
      processData: false,

      success: function(data) {
        $('form[name="emp"]')[0].reset();
        $('#modal-addemp').modal().hide();
        $("#e-empListwrap").load(location.href + "#e-empListwrap");

      },
    }); // AJAX Get Jquery statment

  }
});

HTML

 <div class="e-empListwrap mt-4" id="e-empListwrap"><ul></ul></div>
 // The below is the script when refersh the page then it will call the ajax and get the the and display on the page.
<script>
fetchEMPsaved_data();
function fetchEMPsaved_data(){
   $.ajax({
        url:'controller/developer_control.php',
        type: "post",
        dataType: "json", 
        data: { action: "fetchEMPsaved_data"},
        success: function(data) {
         $("#e-empListwrap ul").append(data);
        }
    });
  }
</script>

【问题讨论】:

  • 这条语句$("#e-empListwrap").load(location.href + "#e-empListwrap"); 要求 JS 将您当前的网页加载到一个元素中。你从你的ajax函数返回data,为什么不直接使用呢?

标签: javascript html jquery ajax


【解决方案1】:

您在 url 之后,在 url 和 CSS 目标之间缺少一个空格:

 $("#e-empListwrap").load(location.href + " #e-empListwrap");

否则 jquery 将其作为 url 的锚点处理,而不是作为元素 ID。您正在加载 url.html#element,但要 grep 元素/css 目标,而不是整个页面,语法应为 url.html #elementOrTarget,中间有空格。

【讨论】:

  • 我一头雾水,这是什么意思? OP 仍在将当前页面加载到页面上的元素中......?也许我误解了什么
  • 他正在加载url.html#element,但是要grep一个元素,而不是整个页面,语法应该是url.html #element,中间有空格。
  • @Daantje,我试过了现在我没有得到整个页面,但很明显我所有的 ajax 响应
  • @Daantje,它现在可以工作了,我已经更改了一些代码。谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2020-09-09
  • 2019-05-11
  • 2012-08-20
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多