使用 Ajax,我可以创建一个函数并将其用于单击事件和 ajax 调用成功吗

Using Ajax, can I create a function and use it for a click event and on success of an ajax call

目前我有以下带有点击事件的按钮对象,使用通常的匿名函数调用。这是为上一个和下一个记录实现的。 ajax 请求只是结束了 运行 一个 mysql 查询和 returning 结果,然后页面被更新

  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() ?

请求的 return 已经发生的事情(promoTable.table.trigger 事情)仍然需要发生。然后需要更新当前的点击事件。

函数就是函数,不管它是匿名的还是命名的。所以如果你有这个:

$('something').click(function () {
    // do something
});

$.someOperation({
    someOption: function () {
        // do something
    }
});

那么你可以简单地这样做:

var doSomething = function () {
    // do something
};

$('something').click(doSomething);

$.someOperation({
    someOption: doSomething
});

函数是 JavaScript 中的第一个 class 公民,因此它们可以像任何其他变量一样传递。

我成功了。不知道为什么我认为它会更复杂

刚做的函数,在点击事件中调用

  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'
 });

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