2015-04-17 29 views
0

我有以下的jQuery(UI)代码:如何“注入”变量到匿名函数

function initSortable() { 
    var placeholderClass = "ui-state-highlight"; 
    var movement; /* = {'id': 0, 'from': 0, 'to': 0};*/ 
    setPlaceholderClassHeight(placeholderClass); 
    $(".listTable td").each(function() { 
     $(this).css("width", $(this).width()); 
    }); 
    $('.listTable tbody').sortable({ 
     placeholder: placeholderClass, 
     forcePlaceholderSize: true, 
     axis: "y", 
     cursor: "grab", 
     opacity: 0.8, 
     start: function (event, ui) { 
      ui.item.toggleClass("highlight"); 
      movement.id = ui.item.data('id'); 
      movement.from = ui.item.data('id'); 
      console.log(ui.item.index()); 
      console.log(ui.item.data('id')); 
     }, 
     stop: function (event, ui) { 
      ui.item.toggleClass("highlight"); 
     }, 
     update: function (event, ui) { 
      console.log(ui.item.index()); 
      movement.to = ui.item.index(); 
      console.log(movement); 
     } 
    }); 
} 

它不工作作为movement是在分配给启动/停止/更新功能未知。 如何使用和更改运动变量?

+2

'运动'声明在正确的地方,它应该更新。它没有更新,因为它是'未定义的'。声明'var movement = {}'并检查 –

+0

thx,它的工作原理! – jeff

+0

将移动到答案:) –

回答

3

movement在正确的位置声明,它应该更新。它不更新,因为它是undefined。声明var movement = {}并检查