jquery - callbacks 101
Before I perform some json and disable a button, I simply want to enable other buttons that may have been previously disabled. I think it's related to callbacks which I apparently still don't get. In the code below, the clicked button does not become disabled prior to running the 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':'dis开发者_StackOverflow中文版abled','value':'Used'});
});
}
A callback is a function you pass to another function. That other function you pass it to must call it - otherwise it's useless.
function someFunctionThatCallsBack(someCallbackFunction) {
// do something
someCallbackFunction(); // calls the callback function
}
Above, you are passing a function to showUseButtons
- but that function just ignores it. It won't be called so whatever you put in there won't do anything.
Perhaps you meant something like the following?
function showUseButtons(callback) {
$(".btnUseProduct").each(function(e) {
$(this).attr({disabled: true, value:'Used'});
});
callback();
}
Further, inside a function the context changes (i.e. the value of this
is different). So you'll need to alter your code somewhat as well:
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){
});
});
Try This...
$(".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'});
});
}
精彩评论