简体   繁体   English

刷新ajax成功的DIV

[英]Refresh a DIV on ajax success

I have a page with has a heading say : 我有一个页面有一个标题说:

<div class="something"><? some php code  ?></div>

In that page I also have an ajax doing a job like: 在那个页面中,我还有一个ajax做一个像这样的工作:

<script>
$(document).ready(function () {
$(document).ajaxStart(function () {
    $("#loader").show();
}).ajaxStop(function () {
    $("#loader").hide();
});
});

$('#link').on('click', function(e) {
 e.preventDefault;
 var href = this.getAttribute('href');  
 $.ajax({
url: href,
success: function(text) {
  alert("Added Succesfully");
}
});
return false; 
});
</script>

Now in the success in ajax i also want to refresh the div i mentioned. 现在在ajax的成功我也想刷新我提到的div。 Only refresh as it is attached to PHP which will fetch data from an external API. 仅刷新,因为它附加到PHP,它将从外部API获取数据。 Is this possible ? 这可能吗 ?

You could put your php code in an external file and reload your div by calling JQuery load method: 您可以将您的PHP代码放在外部文件中,并通过调用JQuery加载方法重新加载您的div:

$("#something").load("mycode.php");

I hope it helps you. 我希望它对你有所帮助。

link 链接

 $.ajax({
    dataType: "json",
    url: "http://www.omdbapi.com/?i=tt0111161",
    success: function (data) {
        console.log(data);
        $("#movie-data").append(JSON.stringify(data));

With the append function you can insert the data from the ajax call into a div. 使用append函数,您可以 ajax调用中的数据插入 div。 explanation and example 解释和例子

The append() method inserts specified content at the end of the selected elements. append()方法在所选元素的末尾插入指定的内容。

Tip: To insert content at the beginning of the selected elements, use the prepend() method. 提示:要在所选元素的开头插入内容,请使用prepend()方法。

Or maybe this answer can fix your issue 或许这个答案可以解决您的问题

or you can take a look at the jquery load function 或者你可以看一下jquery加载函数

Load data from the server and place the returned HTML into the matched element. 从服务器加载数据并将返回的HTML放入匹配的元素中。 This method is the simplest way to fetch data from the server. 此方法是从服务器获取数据的最简单方法。 It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. 它大致相当于$ .get(url,data,success),除了它是一个方法而不是全局函数,它有一个隐式回调函数。 When a successful response is detected (ie when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data. 当检测到成功响应时(即当textStatus为“success”或“notmodified”时),。load()将匹配元素的HTML内容设置为返回的数据。

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

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