简体   繁体   English

如何在ajax调用后重新加载div内容而不重新加载整个页面

[英]How to reload a div content without reloading the whole page after ajax call

what i want to achieve is to reload the content of a div on a page without reloading the whole page, I want to reload the div to after photo upload so the newly uploaded photo can be loaded from the database but i don't want to reload the whole page, but the code was not working instead i got the whole page loading into the div likr below function ajaxLoad(){ 我想要实现的是在页面上重新加载div的内容而不重新加载整个页面,我想在照片上传后重新将div加载到,以便可以从数据库中加载新上传的照片,但是我不想重新加载整个页面,但是代码无法正常工作,我将整个页面加载到函数ajaxLoad(){

     var loadUrl = window.location.href;
    // $(".carouser-inner ").load(loadUrl);
     $(".carouser-inner ").load(loadUrl+".carouser-inner ");
}

I call the function after ajax is successful, but below is what i got 我在ajax成功后调用该函数,但是下面是我得到的

WHAT I WANTED 我想要的 这是我想要获得的,并且ID已从嵌入的数据中获取

But below is what i got, and after i reload the page it now go back to normal as i wanted 但是下面是我得到的,重新加载页面后,现在可以根据需要恢复正常 在此处输入图片说明

Any help on how to achieve this will be appreciated, thanks 感谢您如何实现此目标的任何帮助,谢谢

作为jQuery .load()文档 ,您需要在url和选择器之间一个空格,因此:

$(".carouser-inner ").load(loadUrl+" .carouser-inner");

First clear the div, then set the div's contents. 首先清除div,然后设置div的内容。 You can clear it with a function, maybe clearDiv() or simply set .innerHTML = "" if that's all you need to do. 您可以使用函数clearDiv()清除它,或者只需设置.innerHTML = ""如果您需要这样做)。

Fetching content with AJAX/XHR/Fetch is going to be asynchronous so you will need to architect that as a .Promise and resolve it .then set the content. 抓取内容与AJAX / XHR /取将是异步的,所以你将需要建筑师,作为一个.Promise并解决它.then设置的内容。

this completely depends on what the url you are calling with load() returns. 这完全取决于您使用load()调用的url返回什么。 It seems your url used in load() is returning the entire page's HTML. 看来您在load()中使用的url返回了整个页面的HTML。

Instead you want the url you use with load() to return just your html portion you want to load into the div 相反,您希望与load()一起使用的url只返回要加载到div中的html部分

so there is no problem with your jquery call, it's the value returned by the url used with load() that needs to be fixed. 因此您的jquery调用没有问题,它是与load()一起使用的url返回的值,需要对其进行修复。 That is a server-side question, nothing to do with the jquery code. 那是服务器端的问题,与jquery代码无关。

So, in brief: your fix is to fix the url used with load(), just make it return the partial html you want 因此,简而言之:您的解决方法是修复与load()一起使用的url,只需使其返回所需的部分html。

HTH 高温超导

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

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