每当新的li添加到ul时如何调用函数?

时间:2015-09-08 07:13:54

标签: javascript jquery html jquery-ui html-lists

这是ul将删除新li的地方:

 <ul id="sortable2" class="connectedSortable">
   <li class="ui-state-default li-cat">Appetizers</li>
   <li class="ui-state-default li-cat">Soups</li>
  </ul>

这是ul将被拖动的地方:

 <ul id="sortable1" class="connectedSortable">
   <li class="ui-state-default ap dd">Cobb Salad</li>
   <li class="ui-state-default soup dd">Gazpacho</li>
   <li class="ui-state-default salad dd">Prime Rib</li>
 </ul>

jqueryui函数连接两个列表:

 $(function() {
    $( "#sortable1, #sortable2" ).sortable({
      connectWith: ".connectedSortable",
    }).disableSelection();
  });

我只想在将新元素(li)添加到ul时调用javascript函数。我使用jqueryui进行拖放排序。

1 个答案:

答案 0 :(得分:0)

use change property in jquery sortable, so whenever sortable list is changing this change property will trigger automatically.....
 (function($) {
   var beforeChangeUlLen = $("#sortable2").length;
   $( "#sortable1, #sortable2" ).sortable({
        connectWith: ".connectedSortable",
        change: function(){
              var afterChangeUlLen = $("#sortable2").length;
              if(beforeChangeUlLen > afterChangeUlLen){
                  /*Add you function after new li is added to ul*/
              }
         }
     }).disableSelection();
 })(jQuery);