jQuery:$(this).data()为空
我不知道为什么jQuery:$(this).data()为空,jquery,Jquery,我不知道为什么$(this).data()在按钮单击事件处理程序中返回null,但在firebug中调试时,$(“#“+$(this).attr(“id”)).data()返回我期望的数据 for (var i = 0; i < options.AvailableOperations.length; i++) { var operation = options.AvailableOperations[i]; var button = $(this).find
$(this).data()
在按钮单击事件处理程序中返回null,但在firebug中调试时,$(“#“+$(this).attr(“id”)).data()
返回我期望的数据
for (var i = 0; i < options.AvailableOperations.length; i++) {
var operation = options.AvailableOperations[i];
var button = $(this).find("#btn" + operation);
button.data("operation", operation);
button.data("entityId", options.entityId);
button.parent().removeClass('disabledRevButton');
var data = { operation: operation, entityId: options.entityId };
button.click(function () {
$.postJson({
url: GlobalVars.perfomDFOperation,
data: $(this).data(),
success: function (data) {
if (data != null) {
if (data.IsSuccess && data.RefreshPage) {
location.reload(true);
}
else if (!data.isSuccess) {
alert("error: " + data.Message)
}
}
}
});
});
}
for(变量i=0;i此可能不再指按钮本身,因为它已经在$.postJson()范围内
您可能想尝试使用闭包
button.click(function(){
var myBtn = $(this);
$.postJson({
data: myBtn.data(),
// blah
});
});
我不确定,但值得一试。变量“button”不是您要从中获取数据()的选项吗?使用buton.data(),因为$(this)可以很容易地在过程中引用其他内容
我建议您使用console.log()进行调试,例如,在尝试发布JSON之前,先执行console.log($(this))并查看它引用了什么。“$.postJson()作用域”-不,在将对象传递给postJson之前创建对象