jQuery ajax - 更新div(jQuery ajax - update div)
我正在尝试在jQuery ajax成功请求之后进行更新div块,这是可行的但是所有块dublicates(picture1),在图2中显示了它如何用浏览器刷新页面。
要更新块我正在使用功能:
function async_get(addr,func) {
$.ajax({
url: addr,
async: false,
success: func
});
}
它的调用方式如下:
async_get(
'http://localhost/getfreelance/welcome',
function (data) {
$('#header').html(data);
}
);
有解决方案? :)
你不明白,我需要在ajax请求成功时更新/刷新div块,而不是你的函数fffsuccess()我正在使用: 'function async_get(addr,func) { $.ajax({ url: addr, async: false, success: func }); }' 'function async_get(addr,func) { $.ajax({ url: addr, async: false, success: func }); }'和我的#header div更新,但它会公开数据。
I'm trying to make update div block after jQuery ajax succesfull request, this is works but all blocks dublicates (picture1), in picture 2 is shown how it looks after refreshing page with browser.
To update block I'm using function:
function async_get(addr,func) {
$.ajax({
url: addr,
async: false,
success: func
});
}
and it's called like this:
async_get(
'http://localhost/getfreelance/welcome',
function (data) {
$('#header').html(data);
}
);
Any solutions? :)
You didn't understand, i need to update/refresh div block when ajax request is success, instead of your function fffsuccess() i'm using: 'function async_get(addr,func) { $.ajax({ url: addr, async: false, success: func }); }' and my #header div updates but it dublicates data.
原文:https://stackoverflow.com/questions/11961230
更新时间:2021-05-18 07:05
最满意答案
HTML:
功能:
function fffsuccess(){
alert('success!');
$("#YourImgDivID").html('');
}
function ffferror(){
alert('error!');
$("#YourImgDivID").html('');
}
使用:
$.ajax({
url: 'ajax.cos.php',
type: 'post',
async: false,
success: fffsuccess,
error: ffferror
});
PS你可以替换
$("#YourImgDivID").html('');
至:
$("#YourImgDivID img").attr('src', 'image2.png');
要么
html到:

jQuery:
$("#YourImgDivID img#YourImgID").attr('src', 'image2.png');
随你
html:
function:
function fffsuccess(){
alert('success!');
$("#YourImgDivID").html('');
}
function ffferror(){
alert('error!');
$("#YourImgDivID").html('');
}
use:
$.ajax({
url: 'ajax.cos.php',
type: 'post',
async: false,
success: fffsuccess,
error: ffferror
});
P.S. you can replace
$("#YourImgDivID").html('');
to:
$("#YourImgDivID img").attr('src', 'image2.png');
or
html to:

jQuery to:
$("#YourImgDivID img#YourImgID").attr('src', 'image2.png');
whatever
相关问答
我要为用户使用一个链接来选择一个Pin,但你明白了: #:remote => true allows ajax stuffz.
true %>
在comments_controller.rb(我在这里使用索引操作,但是根据需要调整) def index
@pin = Pin.find(params[:pin_id])
@comments = @pin.comm
...
根据需要添加一些代码示例 假设您的JSON是 [{"targetElement":"div1","HtmlContent":"
jQuery是 $(document).ready(function() {
$('.submitForm').ajaxForm({
success: function(returnData) {
$(returnData.targetElement).html(returnData.HtmlCo
...
$("#step1Content").load('YourUrl');
Solved.Thank you all. Its my fault. It was very easy. Its solution is just prepend hole
可以说是比詹姆斯斯基德莫尔的答案更清洁的解决方案,尽管这个想法是一样的: var $holder = $('
$('#coupon').html($('#coupon', $holder).html());
$('#order-summary').html($('#order-summary', $holder).html());
An arguably cleaner solution than James Skidmore's answ
...
我希望了解你的问题.....我猜你的第一个ajax调用是在文档准备就绪时运行的吗? 然后第二个ajax调用在用户点击div时运行? 但是你的问题是你的第一个ajax调用是在用户点击时调用的吗? 如果是这样,这将解决它: 我认为简单的事情是创建一个变量“firstAjaxRan”,将其设置为false。 当第一个ajax调用运行时,检查“firstAjaxRan”是否为true,如果不是则运行它然后将其设置为true。 这将阻止第一个ajax调用在用户单击div时再次运行。 你的第二个ajax应该运
...
对于GET请求,不应该有数据部分,将其作为查询字符串,如下面的js代码: $("#submit").click(function() {
var food = $("#food").val();
var amount = $("#amount").val();
$.ajax({
url: 'search_value.php?food='+ food + '&amount='+amount,
type: 'GET',
datatype: "html",
...
只需从ajax调用中获取返回的数据并将其插入到元素中: $(document).ready(function(){
setInterval(refreshDiv, 5000);
});
function refreshDiv(){
$.ajax({
url: 'editStatus.jsp'
}).done(function(result) {
$('#refreshDIV').text(result);
});
}
Just
...
HTML:
功能: function fffsuccess(){
alert('success!');
$("#YourImgDivID").html('');
}
function ffferror(){
alert('error!');
$("#YourImgDivID").html('');
}
使用:
...