在我执行一些json并禁用按钮之前,我只想启用之前可能已禁用的其他按钮。我认为这与回调相关,我显然还是没有。在下面的代码中,在运行getJSON之前,单击的按钮不会被禁用。
$(".btnUseProduct").live("click", function(e) {
var clickedId = $(this).attr('id');
var provisioning_id = clickedId.split("^")[0];
var entity_id = clickedId.split("^")[1];
var entity = clickedId.split("^")[2];
showUseButtons(function(){
$(this).attr({'disabled':'disabled','value':'Used'}); //doesn't work
$.getJSON("/chinabuy-new/cfcs/services.cfc?method=update_provisioning&returnformat=json&queryformat=column",{"provisioning_id":provisioning_id,"entity_id":entity_id},function(res,code){
});
});
e.preventDefault();
});
function showUseButtons() {
$(".btnUseProduct").each(function(e) {
$(this).attr({'disabled':'disabled','value':'Used'});
});
}
答案 0 :(得分:2)
回调是您传递给另一个函数的函数。你传递给它的其他功能必须调用它 - 否则它就没用了。
function someFunctionThatCallsBack(someCallbackFunction) {
// do something
someCallbackFunction(); // calls the callback function
}
上面,您将函数传递给showUseButtons
- 但该函数只是忽略它。它不会被调用,因此无论你放在哪里都不会做任何事情。
也许你的意思如下:
function showUseButtons(callback) {
$(".btnUseProduct").each(function(e) {
$(this).attr({disabled: true, value:'Used'});
});
callback();
}
此外,在函数内部,上下文发生变化(即this
的值不同)。因此,您还需要稍微改变一下代码:
var _this = this; // save the context - the clicked DOM element
showUseButtons(function(){
$(_this).attr({disabled:true, value:'Used'}); //doesn't work
$.getJSON("/chinabuy-new/cfcs/services.cfc?method=update_provisioning&returnformat=json&queryformat=column",{"provisioning_id":provisioning_id,"entity_id":entity_id},function(res,code){
});
});
答案 1 :(得分:-1)
试试这个...
$(".btnUseProduct").live("click", function(e) {
var clickedId = $(this).attr('id');
var provisioning_id = clickedId.split("^")[0];
var entity_id = clickedId.split("^")[1];
var entity = clickedId.split("^")[2];
showUseButtons(function(){
$(this).attr({'disabled':'disabled','value':'Used'}); //doesn't work
$.getJSON("/chinabuy-new/cfcs/services.cfc?method=update_provisioning&returnformat=json&queryformat=column",{"provisioning_id":provisioning_id,"entity_id":entity_id},function(res,code){
//give class name common for all buttons and make all to hide
$("input.button_class_names").css({'visibility' : 'visible'});
//make ur pressed button visible like this
clickedId.css({'visibility' : 'hidden'});
});
});
e.preventDefault();
});
function showUseButtons() {
$(".btnUseProduct").each(function(e) {
$(this).attr({'disabled':'disabled','value':'Used'});
});
}