2012-08-01 65 views
2

現在,我使用jQuery庫和我所面臨的問題如下因素: Supppose我有排序列表,如果項目爲了改變事件jQuery的排序

<html> 
     <head> 
     <script src="http://code.jquery.com/jquery-latest.js"></script> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js> </script> 
     <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 
     <script> 
      $(document).ready(function(){ 
       jQuery(".block").sortable({ 
       axis: 'y' 
      }); 
      }); 
     </script> 

     </head> 
     <body> 
      <ul class="block" type="none"> 
       <li>1</li> 
       <li>2</li> 
       <li>3</li> 
      </ul> 
     </body> 
    </html> 

所以ofter順序改變,我怎樣才能使偵聽這個事件,我的意思是,有任何訂單更改事件的聽衆或類似的東西。

+1

雅!有檢查jquery ui的排序文件。 – SVS 2012-08-01 07:20:15

回答

5

它正好在文檔中。使用changeupdate事件:

$(document).ready(function(){ 
    $(".block").sortable({ 
     axis: 'y', 
     change: function(event, ui) { 
      console.log('change', event, ui); 
     }, 
     update: function(event, ui) { 
      console.log('update', event, ui); 
     } 
    }); 
}); 

而且無論是使用jQuery$,不混合。

+2

我認爲['update'](http://jqueryui.com/demos/sortable/#event-update)可能更適合OP的需求:'當用戶停止排序並且DOM位置有changed.'。 – 2012-08-01 07:22:58

+1

@FabrícioMatté你是對的,補充說。 – 2012-08-01 07:27:09

+0

謝謝你,爲我工作。 – 2012-08-01 07:53:09

1

是,

$(".selector").bind("sort", function(event, ui) { 
    ... 
}); 
documentation

直接。

+0

我的新口頭禪,「在提問前閱讀文檔」,非常感謝 – 2012-08-01 07:54:43