【发布时间】:2018-08-30 17:20:35
【问题描述】:
我有以下用户界面:
从那里我需要动态更改按钮 Archive 和 Unarchive 以及来自 FontAwesome 的图标,具体取决于所采取的操作和后端的响应。响应基本上是这样的:
{"row_id":"12518","status":true}
简而言之:
- 在蓝色行(存档项)中:如果我点击
Unarchive按钮并且响应为true我应该:删除背景颜色,将按钮文本更改为Archive,将按钮类从@ 更改将 987654331@ 更改为archive-form并将 FontAwesome 图标类从fa fa-arrow-circle-up更改为fa fa-arrow-circle-down。 - 在另一行(非归档项目):如果我点击
Archive并且响应为true,那么我应该:添加蓝色背景,将按钮文本更改为Unarchive,将按钮类从archive-form到unarchive-form并将 FontAwesome 图标类从fa fa-arrow-circle-down更改为fa fa-arrow-circle-up。
我几乎涵盖了所有内容(我将只显示一个案例的来源,因为它几乎相同,我不想发表大篇幅):
$.ajax({
url: '/ajax/forms/archive',
type: 'POST',
dataType: 'json',
data: {
form_id: ids
}
}).done(function (response) {
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr'),
this_btn = this_row_tr.find('.archive-form');
if (value.status === true) {
// here I add the background color to the TR
this_row_tr.addClass('info');
// here I swap the button class
this_btn.removeClass('archive-form').addClass('unarchive-form');
// here I swap the button text and also the whole FontAwesome part
this_btn.text('<i class="fa fa-arrow-circle-up" aria-hidden="true"></i> Unarchive');
} else if (value.status === false) {
this_row_tr.addClass('error');
all_archived = false;
}
});
if (all_archived) {
toastr["success"]("The forms were archived successfully.", "Information");
} else {
toastr["error"]("Something went wrong, the forms could not be archived.", "Error");
}
}).error(function (response) {
console.log(response);
toastr["error"]("Something went wrong, the forms could not be archived.", "Error");
});
但是当我更改 FontAwesome 的文本时问题就来了,因为我得到的是纯文本而不是图标。例如点击第一行的Unarchive会变成这样:
如您所见,除了 FontAwesome 图标外,一切都很好。有什么方法可以刷新按钮以使更改生效?或者您知道其他方法可以实现这一目标吗?
【问题讨论】:
-
能否请您也添加回复文本?
-
@AlpeshJikadra 完成,在 OP 上
-
HTML 在浏览器中呈现后能否显示?我没有看到用于生成行的脚本与问题相关;除非我遗漏了一些我认为我们需要看到的是:html(您想要修改/操作的)、ajax 脚本和事件处理程序,用于启动和处理对后端的调用和响应文本。
-
@DavidThomas HTML 代码的最后一个 sn-p 是呈现代码的样子。我只是添加了
$.each循环,但我将编辑 OP 并删除不必要的内容并添加您要问的内容
标签: javascript jquery