目前,我使用通常的匿名函数调用,具有单击事件的以下按钮对象。这是针对上一个和下一个记录实现的。 ajax请求只是最终运行mysql查询并返回结果,然后页面更新
ticket_change_buttons.click(function(){
if(ticket.id.val() != ''){
options = {
operation: 'get_relative_ticket',
ticket_id: ticket.id.val(),
direction: null
}
if($(this).is($('#next_code'))){
options.direction = 'true';
}
if($(this).is($('#prev_code'))){
options.direction = 'false';
}
request(options,function(data){
if(!data){return;}
ticket.id.val(data.submitid);
current_id = ticket.id.val();
ticket.status.val(data.help_status);
ticket.sub_date.val(data.submit_date);
ticket.promo_code.val(data.promo_code);
ticket.fname.val(data.firstname);
ticket.phone.val(data.phone);
ticket.zip.val(data.zip_code);
ticket.acc_num.val(data.account_number);
ticket.lname.val(data.lastname);
ticket.email.val(data.email);
ticket.msg.val(data.message);
/*enable all the inputs*/
ticket.status.prop('disabled',false);
ticket.sub_date.removeAttr('disabled');
ticket.promo_code.removeAttr('disabled');
ticket.fname.removeAttr('disabled');
ticket.phone.removeAttr('disabled');
ticket.zip.removeAttr('disabled');
ticket.acc_num.removeAttr('disabled');
ticket.lname.removeAttr('disabled');
ticket.email.removeAttr('disabled');
ticket.msg.removeAttr('disabled');
});
}
});

页面上有第二个按钮,打开以下对话框,然后发出不同的ajax请求。以下是该对话框的代码:
notify.resolve_message.window.dialog({
autoOpen: false,
buttons: {
"Resolve": function() {
var options = {
operation : 'resolve_ticket_only',
ticket_id : notify.resolve_message.ticket_id.text()
};
$(this).dialog("close");
request(options, function(data){
promoTable.table.trigger("update").trigger("sorton", promoTable.table.get(0).config.sortList).trigger("appendCache").trigger("applyWidgets");
return;
});
},
Cancel: function() {
$(this).dialog("close");
}
},
width: '440px'
});

我怎样才能(如果可能的话)从ticket_change_buttons.click(function()中获取匿名函数,并使其成为一个可以在对话框中单击和成功请求后使用的函数?
返回请求时已经发生的事情(promoTable.table.trigger)仍然需要发生。然后,需要进行当前点击事件的更新。
答案 0 :(得分:1)
函数是一个函数,无论它是匿名还是命名。所以如果你有这个:
$('something').click(function () {
// do something
});
$.someOperation({
someOption: function () {
// do something
}
});
然后你可以这样做:
var doSomething = function () {
// do something
};
$('something').click(doSomething);
$.someOperation({
someOption: doSomething
});
函数是JavaScript中的一等公民,所以它们可以像任何其他变量一样传递。
答案 1 :(得分:0)
我得到了这个工作。不知道为什么我认为它会更复杂
刚刚创建了该函数并在click事件中调用它
ticket_change_buttons.click(prevNext);
function prevNext(){
if(ticket.id.val() != ''){
options = {
operation: 'get_relative_ticket',
ticket_id: ticket.id.val(),
direction: null
}
// if($(this).is($('#next_code'))){
// options.direction = 'true';
// }
if($(this).is($('#prev_code'))){
options.direction = 'false';
} else {
options.direction = 'true';
}
request(options,function(data){
if(!data){return;}
ticket.id.val(data.submitid);
current_id = ticket.id.val();
ticket.status.val(data.help_status);
ticket.sub_date.val(data.submit_date);
ticket.promo_code.val(data.promo_code);
ticket.fname.val(data.firstname);
ticket.phone.val(data.phone);
ticket.zip.val(data.zip_code);
ticket.acc_num.val(data.account_number);
ticket.lname.val(data.lastname);
ticket.email.val(data.email);
ticket.msg.val(data.message);
/*enable all the inputs*/
ticket.status.prop('disabled',false);
ticket.sub_date.removeAttr('disabled');
ticket.promo_code.removeAttr('disabled');
ticket.fname.removeAttr('disabled');
ticket.phone.removeAttr('disabled');
ticket.zip.removeAttr('disabled');
ticket.acc_num.removeAttr('disabled');
ticket.lname.removeAttr('disabled');
ticket.email.removeAttr('disabled');
ticket.msg.removeAttr('disabled');
});
}
};

然后在触发器之后简单地调用它:
notify.resolve_message.window.dialog({
autoOpen: false,
buttons: {
"Resolve": function() {
var options = {
operation : 'resolve_ticket_only',
ticket_id : notify.resolve_message.ticket_id.text()
};
$(this).dialog("close");
request(options, function(data){
promoTable.table.trigger("update").trigger("sorton", promoTable.table.get(0).config.sortList).trigger("appendCache").trigger("applyWidgets");
prevNext();
return;
});
},
Cancel: function() {
$(this).dialog("close");
}
},
width: '440px'
});

如果有更好的方法,请告诉我。