如何将click事件添加到jstree的(jQuery插件)异步列表?

Dyl*_*SUN 3 javascript php jquery jstree

我想将click事件添加到jstree的异步列表项中.

理想的结果是:当我单击jstree中的项目时,项目的内容将作为参数转移到sql查询,然后执行查询并在同一页面或另一页面中显示结果集.

虽然我不知道如何实现它.我在jquery.tree.js中找到了以下代码.我想我应该修改这个事件.但我不知道怎么做.你能看到代码并给我一些建议或指导吗?

$("#" + this.container.attr("id") + " li a")
 .live("click.jstree", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON
  if(event.which && event.which == 3) return true;
  if(_this.locked) {
   event.preventDefault(); 
   event.target.blur();
   return _this.error("LOCKED");
  }
  _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
  if(_this.inp) { _this.inp.blur(); }
   event.preventDefault();
   event.target.blur();
   return false;
  })
Run Code Online (Sandbox Code Playgroud)

页面代码:

<script type="text/javascript" >
    $(function () { 
        $("#async_json_1").tree({
            data : { 
                type : "json",
                opts : {
                    url : "twodimen.php"
                }
            },
            callback:{
                onselect: function(node,tree){

                }
            }
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

非常感谢.

har*_*pax 5

您可以使用回调方法onselect,通常在单击节点时进行(即使您也可以通过脚本选择它)

如果您的节点(li)具有"node_1234"形式的id,则:

<script type="text/javascript" >
 $(function () { 
  $("#async_json_1").tree({
   data : { 
    type : "json",
    opts : {
     url : "twodimen.php"
    }
   },
   callback: {
      onselect: function(node, tree) {
         var id = $(node).attr("id").split("_")[1]; // that is 1234
         $.ajax({
            url: "your/url",
            data: "id="+id,
            success: function(data){
               alert("Great");
            }
         });
      }
   }
  });
 });
</script>
Run Code Online (Sandbox Code Playgroud)

我刚才意识到,还有一种更简单的方法可以满足您的需求:

<script type="text/javascript" >
 $(function () { 
  $("#async_json_1").tree({
   data : { 
    type : "json",
    opts : {
     url : "twodimen.php"
    }
   }
  });
  $(".tree a").live("click", function(e) {
     // your code here
  }) 
 });
</script>
Run Code Online (Sandbox Code Playgroud)