2012-06-28 21 views
1

我有這個功能,但它給了我函數聲明的名稱錯誤需要名稱的onStop功能函數語句需要在JQuery中

<script type="text/javascript"> 
jQuery(function($) { 
    $('#Hnav, #Nnav').NestedSortable(
      { 
       accept: 'sort', 
       noNestingClass: "no-children", 
       helperclass: 'helper', 
       autoScroll: true, 
       onChange: function(serialized) { 
       onStop : function(){ 
        $('#output').html($(this).id); 
       }, 
        nestingPxSpace : '0' 
      } 
    ); 
}); 
</script> 
+4

您沒有正確關閉的onChange功能所以丟失的支架 – devnull69

回答

1

有一個支架失蹤,你用錯誤的語法越來越ID試試這個,

$('#output').html($(this).id); 

應該

$('#output').html(this.id); 

$('#output').html($(this).attr(id)); 

    jQuery(function($) { 
    $('#Hnav, #Nnav').NestedSortable({ 
     accept: 'sort', 
     noNestingClass: "no-children", 
     helperclass: 'helper', 
     autoScroll: true, 
     onChange: function(serialized) { 
      alert("changed");// Changed to fix 
     },    
     onStop: function() { 
      $('#output').html(this.id); 
     }, 
     nestingPxSpace: '0'  
    }); 
});​ 
+2

錯了位置,感謝@ devnull69 – devnull69

+0

剛剛修正的括號匹配 – Adil

+1

沒有它不...括號在onStop參數前缺少 – devnull69

4

在這種情況下,this指的是發生事件的DOM元素。一個DOM元素不是一個jQuery對象。

jQuery $(this)對象沒有id屬性,而DOM元素的確如此。因此,使用:

$('#output').html(this.id); 

或:

$('#output').html($(this).attr("id")); 

而且不要忘記關閉支架在onChange處理函數。

+0

答案會更好,如果你解釋*爲什麼* –

+0

@FlorianMargaine不能做得更好:) – VisioN

+0

我可以嗎? :-我只是加上'this'是什麼,所以*爲什麼*一個工作,而另一個不工作。 –

1

集體中有兩個代碼問題,

  1. 內部函數沒有正確關閉。
  2. jQuery選擇器不支持的元素的id屬性。

修改後的代碼是,

jQuery(function($) { 
    $('#Hnav, #Nnav').NestedSortable(
      { 
       accept: 'sort', 
       noNestingClass: "no-children", 
       helperclass: 'helper', 
       autoScroll: true, 
       onChange: function(serialized) { 
        //A empty function 
       }, 
       onStop : function(){ 
        $('#output').html($(this).attr("id")); 
       }, 
       nestingPxSpace : '0' 
      } 
    ); 
}); 
+0

還有一個JS的問題,當我加載它說jQuery(「#」+ s)的頁面。get(0)未定義 –

+0

至於此代碼被認爲沒有問題,現在這個問題是與您在代碼中使用的NestedSortable javasciprt文件。同時檢查HTML #Hnav,#Nnav這也可能是一個問題。 –

+0

感謝您的幫助,我解決了它。我有一個問題可以幫我嗎? –