![](/img/trans.png)
[英]Updating global variable after ajax function call with jQuery
[英]Updating HTML after Ajax or jQuery call
我创建了两个调用,一个是对控制器的ajax调用,另一个是jquery调用。 我都只是为了检查触发事件后我的HTML是否会更新...但是这些方法都没有更新我的HTML(它保持不变)。 方法如下:
方法1:
$("#btnSearch").click(function(){
var startDate = $.trim($("#startDate").val());
var endDate = $.trim($("#endDate").val());
$.post("/history/list",{'startDate':startDate,"endDate":endDate},function(data){
var result = $('<div />').append(data).find('#historyList').html();
$('#historyList').append(result);
console.log(result);
// the result is displayed correctly in the console...
});
});
// This is supposed to update my HTML in the browser now, but it's not showing anything new...
方法2:
$('#btnSearch').click(function () {
console.clear();
$.ajax({
url: "/history/list",
data: {
startDate: $('#startDate').val(),
endDate: $('#endDate').val(),
},
type: "GET",
dataType: "html",
success: function (data) {
var result = $('<div />').append(data).find('#historyList').html();
$('#historyList').append(result);
console.log(result);
// the result is displayed correctly in the console...
},
error: function (xhr, status) {
alert("Sorry, there was a problem!");
},
complete: function (xhr, status) {
//$('#showresults').slideDown('slow')
}
});
});
一旦调用完成并返回数据,这些方法都不会更新我的HTML ...数据对象保存HTML作为结果,我希望数据对象中的HTML替换id #下div标签中的所有内容。 historyList ..我在这里做错了什么?
PS我正在使用Chrome浏览器(如果这是有用的信息)
我没有得到结果。 您是否可以使用.html,像这样:
$('#historyList').html(data);
或像这样的整个代码。
$('#btnSearch').click(function () {
console.clear();
$.ajax({
url: "/history/list",
data: {
startDate: $('#startDate').val(),
endDate: $('#endDate').val(),
},
type: "GET",
dataType: "html",
success: function (data) {
$('#historyList').html(data);
console.log(result);
// the result is displayed correctly in the console...
},
error: function (xhr, status) {
alert("Sorry, there was a problem!");
},
complete: function (xhr, status) {
//$('#showresults').slideDown('slow')
}
});
});
确保$('#historyList')
不是空数组,并且存在于DOM中。 如果有的话
$('#historyList').html(data);
应该解决问题。
如果我对问题的理解是错误的,请纠正我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.