如何在AJAX请求后使用Jquery替换元素的文本
How to replace text of element after AJAX request using Jquery
我使用Jquery V1.11.1,我想在Jquery AJAX请求后替换HTML表中的单元格。我想要单元格中的结果。
$(".permission").click(function(e) {
var acoPath = $(this).siblings('th').text();
var rowsLeft = $(this).prevUntil("th").length;
var aroGroup = $('.aro').eq(rowsLeft).text();
$.ajax({
url: "/permissions/update",
type: "POST",
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
$(this).html(html);
}
});
});
});
当我单击所需单元格中的文本时,没有任何内容被替换。我怎样才能解决这个问题?
您需要将ajax调用中的上下文设置为当前对象。您可以使用ajax中的上下文选项:
context: this,
修改ajax调用:
$.ajax({
url: "/permissions/update",
type: "POST",
context: this,
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
$(this).html(html);
}
});
您的上下文已更改。在‘click
’功能中,this
引用按钮。但是在回调函数中,上下文发生了变化。您应该首先将上下文保存到一个变量(var self
)。
$(".permission").click(function(e) {
var acoPath = $(this).siblings('th').text();
var rowsLeft = $(this).prevUntil("th").length;
var aroGroup = $('.aro').eq(rowsLeft).text();
var self = this;
$.ajax({
url: "/permissions/update",
type: "POST",
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
self.html(html);
}
});
});
});
相关文章:
- 如何用javascript替换Jquery字符串上的新参数
- 替换jquery对象中键的值
- 替换jquery中的spanhtml文本
- 替换 jQuery 中锚标记的值
- 替换 jquery 加载的外部 html 文件中的文本
- 在 .click() 事件到 .get() 页面上并替换 jquery 中的 .html()
- 如何使用 ?parameter=only 链接重新加载页面并替换 Jquery Mobile 中的当前锚页面
- 香草 js 替换 jQuery 的 index() 和 eq()
- JavaScript-用Angular代码替换jQuery$
- 用我自己的(措辞?)替换jquery函数
- 使用正则表达式替换Jquery
- 图像悬停并替换-jquery
- 替换jquery's仅在加载DOM功能后运行
- 替换jQuery单击事件上的标题文本
- 如何动态替换jQuery MOBILE中的过滤器搜索栏?
- 动画img替换jquery悬停
- 替换jquery中的多个查找选择器
- 找到,替换jquery..搜索词数组有问题
- Javascript/jsLint:使用“use strict”时用什么替换jQuery(this);
- 用Javascript替换jQuery函数