2016-03-23 29 views
0

我做了一個AJAX調用來創建和填充我的JSTree div。我激活了複選框插件並嘗試檢查我的AJAX結果回調中的所有複選框。它不起作用。Jstree - 無法檢查AJAX調用中的複選框,適用於傳統調用。一個問題?

我綁定了相同的功能(這將檢查所有框)到一個按鈕,在這裏,它的工作原理。

我也試圖通過一個自定義事件使用jQuery檢查所有的複選框,但它不會做任何事情,如AJAX調用

這是一個問題與JSTree還是有,我是什麼失蹤?

我轉載此問題的jsfiddle(?):https://jsfiddle.net/Lyyn/c74wpa6z/


規格:

  • 的jQuery 1.9.1
  • jsTree 3.2.1

代碼

HTML

<h1> 
JSTree Demo 
</h1> 
<button id="btn"> 
Check All 
</button> 
<div id="jstree"> 

</div> 

JS

// Populate the tree with some generic data 
$.ajax({ 
    // ... initial ajax code with some generic data 
    success: function(response) { 
      var $tree = $("#jstree"); 
      $tree.jstree(response); 

     // Try to check all boxes, it doesn't work 
     checkAll($tree); 
    } 
}); 

// Try to check all boxes, here it works. Why. 
$("#btn").click(function(){ 
     checkAll($("#jstree")); 
}) 

// This will check all boxes inside the tree 
function checkAll(tree) { 
     tree.jstree(true).check_all(); 
} 

回答

1

這是因爲jstree尚未建成的節點,當你調用checkAll成功回調中。只需將您的checkAll電話打入ready.jstree事件如下。檢查演示 - Fiddle

... 
success: function(response) { 
    var $tree = $("#jstree"); 
    $tree.jstree(response).on('ready.jstree', function() { 

     // Try to check all boxes 
     checkAll($tree); 

}); 
+0

我明白了......非常感謝,它工作得很好! –