jQuery fadeOut回调错误
jQuery fadeOut callback error
Firebug表示我有一个TypeError: a.ownerDocument is undefined
,代码为:
$(this).text('Updated!').fadeOut('900', function() {
$(this).text('Update').attr('disabled', 'disabled').show()
});
下面是一个更完整的片段:
$(".update-role").click(function() {
var newRole = $(this).prev().val();
var userId = $(this).parents('tr').attr('id'); // e.g. 'user-role-18'
userId = userId.split('-');
userId = userId[2]; // so we get '18'
$.getJSON('services/update_staff_role.php', {r: newRole, id: userId}, function(j) {
if(j.result == 'success') {
$(this).text('Updated!').fadeOut('900', function() {
$(this).text('Update').attr('disabled', 'disabled').show()
});
} else {
alert(j.reason);
}
});
console.info(userId, newRole);
});
可能是因为我在使用this
,还是因为我在另一个callback
中使用callback
?
添加var _this = $(this);
,然后使用_this.text...
而不是$(this).text..
,因为$(this)
指的是$.getJSON
:
$(".update-role").click(function() {
var _this = $(this);
var newRole = $(this).prev().val();
var userId = $(this).parents('tr').attr('id');
userId = userId.split('-');
userId = userId[2];
$.getJSON('services/update_staff_role.php', {r: newRole, id: userId}, function(j) {
if(j.result == 'success') {
_this.text('Updated!').fadeOut('900', function() {
$(this).text('Update').attr('disabled', 'disabled').show()
});
} else {
alert(j.reason);
}
});
console.info(userId, newRole);
});
是的,this
在回调中不是指单击的按钮。在调用$.getJSON之前,您可以将按钮引用存储在一个变量中,然后在回调中访问该变量。
相关文章:
- 测试Angular Service解决错误回调中的promise
- $http服务未触发成功或错误回调
- jQuery AJAX错误回调未运行
- JavaScriptajax请求servlet调用错误回调函数
- 为什么 AJAX 完成和错误回调都触发了
- Cordova Android短信插件不会触发成功或错误回调
- 无法识别错误回调
- 在第一次错误回调时退出承诺链
- 如何获取 Ember 组件对象内部承诺成功或错误回调
- 在 Node.js 中传播错误回调,并避免所有额外的麻烦
- 在 HTTP 错误回调中访问“this”
- 在 AngularJS 中声明一个承诺,并带有命名的成功/错误回调
- 谷歌浏览器中的地理位置错误回调
- PhoneGap 3.0.0 Tanelih 蓝牙插件在 Android 上已启用错误回调不起作用
- $http错误回调不能与 httpProvider responseInterceptor 结合使用
- JayData 上的默认错误回调
- 错误回调中的错误:TestPlugin2 = 引用错误:找不到变量:在 file:///android_asset/ww
- 主干.js:保存方法始终返回错误回调
- AJAX 中的成功或错误回调不起作用
- Backbone.js:奇怪的.save()错误回调