I have problem with callback function I think that this is reason why I have window attribute when I click in button. This is code
我有回调函数的问题我认为这是我点击按钮时有窗口属性的原因。这是代码
function removeProduct(callback) {
var recordchosen = $(this); ////////here I have WINDOW instead button
var recordToDelete = recordchosen.attr("data-id");
if (recordToDelete != '') {
// Send post request with AJAX
$.post("/Cart/RemoveFromCart", { "albumID": recordToDelete },
function (response) {
// Success
$('#productCart-' + response.prodID).fadeOut('slow', function () {
if (response.productsCart == 0) {
$("#cartEmpty").removeClass("hidden");
}
});
var textCart = $("#cartbox").text();
var amountCart = (parseInt(textCart) - 1);
$("#cartbox").text(amountCart);
});
callback();
return false;
};
};
$(".removeProduct").click(function () { removeProduct(updateTotal) });
Someone know what I do wrong?
有人知道我做错了什么吗?
1 个解决方案
#1
0
The short answer is that this
is losing context because the button is not directly invoking removeProduct
. Instead, you are passing an anonymous function which then calls removeProduct
. In non-strict mode, javascript will coerce an undefined value of this
into window
hence your issue.
简短的回答是,这会丢失上下文,因为按钮不是直接调用removeProduct。相反,您传递的是匿名函数,然后调用removeProduct。在非严格模式下,javascript会将此未定义的值强制转换为窗口,因此您的问题。
To solve it you can use the call approach as mentioned in the comments, although I would make one adjustment:
要解决它,你可以使用评论中提到的调用方法,虽然我会做一个调整:
$(".removeProduct").click(function () {
removeProduct.call(this, updateTotal);
});
Since the anonymous function has the proper context of this, we can call removeProduct
using this
from the anonymous function.
由于匿名函数具有适当的上下文,我们可以使用匿名函数调用removeProduct。
I don't want to go into much more detail since this issue is very well documented on this site and online in general, but if you want to read more about how call
works here is the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
我不想详细介绍这个问题,因为这个问题在本网站和网上都有很好的记录,但是如果你想了解更多关于call如何工作的文档:https://developer.mozilla。组织/ EN-US /文档/网络/的JavaScript /参考/ Global_Objects /功能/通话
#1
0
The short answer is that this
is losing context because the button is not directly invoking removeProduct
. Instead, you are passing an anonymous function which then calls removeProduct
. In non-strict mode, javascript will coerce an undefined value of this
into window
hence your issue.
简短的回答是,这会丢失上下文,因为按钮不是直接调用removeProduct。相反,您传递的是匿名函数,然后调用removeProduct。在非严格模式下,javascript会将此未定义的值强制转换为窗口,因此您的问题。
To solve it you can use the call approach as mentioned in the comments, although I would make one adjustment:
要解决它,你可以使用评论中提到的调用方法,虽然我会做一个调整:
$(".removeProduct").click(function () {
removeProduct.call(this, updateTotal);
});
Since the anonymous function has the proper context of this, we can call removeProduct
using this
from the anonymous function.
由于匿名函数具有适当的上下文,我们可以使用匿名函数调用removeProduct。
I don't want to go into much more detail since this issue is very well documented on this site and online in general, but if you want to read more about how call
works here is the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
我不想详细介绍这个问题,因为这个问题在本网站和网上都有很好的记录,但是如果你想了解更多关于call如何工作的文档:https://developer.mozilla。组织/ EN-US /文档/网络/的JavaScript /参考/ Global_Objects /功能/通话