jQuery 获取 jsTree 的检查值 - 使用表单提交提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6048001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:17:51  来源:igfitidea点击:

get checked values for jsTree - submit with form post

jqueryjquery-pluginsjstree

提问by dzm

I'm using the jsTree jQuery plugin with the checkbox theme. Does anyone know how to get the selected values with a form post?

我正在使用带有复选框主题的 jsTree jQuery 插件。有谁知道如何通过表单帖子获取选定的值?

Thank you!

谢谢!

采纳答案by soumya

Have you got your answer ? If not, here is one that appears in the jstree google groups

你得到答案了吗?如果没有,这是出现在jstree google 组中的一个

    function submitMe(){ 
        var checked_ids = []; 
        $("#server_tree").jstree("get_checked",null,true).each 
            (function () { 
                checked_ids.push(this.id); 
            }); 
           doStuff(checked_ids); 

回答by Lukas Jelinek

In the last version (3.0), the API was changed.

在上一个版本 (3.0) 中,API 发生了变化。

If you need just array of selected IDs (like in examples in this node), it is now very easy:

如果您只需要选定 ID 的数组(如本节点中的示例),现在非常简单:

var selectedElmsIds = $('#tree').jstree("get_selected");

If you need to iterate over the selected elements, you just need to pass additional "true" parameter.

如果您需要迭代所选元素,您只需要传递额外的“true”参数。

var selectedElmsIds = [];
var selectedElms = $('#tree').jstree("get_selected", true);
$.each(selectedElms, function() {
    selectedElmsIds.push(this.id);
});

回答by Jamshid Hashimi

Everyone, who worked with Jstree's may face to this question: How to get the checked Ids of Jstree in form submit? here is the solution:

使用过Jstree's 的大家可能会面临这样一个问题:如何在表单提交中获取Jstree 的checked id?这是解决方案:

function submitMe() {
    var checked_ids = [];
    $('#your-tree-id').jstree("get_checked",null,true).each(function(){
        checked_ids.push(this.id);
    });
    //setting to hidden field
    document.getElementById('jsfields').value = checked_ids.join(",");
}

Now, we set it in a hidden field:

现在,我们将其设置在一个隐藏字段中:

<input type="hidden" name="jsfields" id="jsfields" value="" />

回答by soumya

The suggested solution from google groups however didn't work for partially checked nodes, in my case. I had to leave the get_checked out and do the following to get fully selected and partially selected nodes.

但是,在我的情况下,来自 google 组的建议解决方案不适用于部分检查的节点。我不得不离开 get_checked 并执行以下操作以获得完全选择和部分选择的节点。

$(".sector-tree").find(".jstree-undetermined").each(function(i,element){ checked_ids.push($(element).attr("id")); if ($(this).find(".jstree-undetermined").length == 0) { $(this).find(".jstree-checked").each(function(i, element){ checked_ids.push({$(element).attr("id")); }); } }); // collect the rest of the checked nodes that exist under checked parents $(".sector-tree").find(".jstree-checked").each(function(i, element){ //also includes the ones below 'undetermined' parent var selectedElement = $(element).attr("id"); if ( hasItem(selectedElement, checked_ids ) < 0 ) { checked_ids.push(selectedElement); } });

回答by Fostah

With jQueryyou can simply do:

有了jQuery你可以简单地这样做:

$('.jstree-checked,.jstree-undetermined').each(function(){
    var rawCheckedID = $(this).find('a').attr('id');
});

This will get the undetermined and checked at the same time. soumya's solution above may be more efficient.

这将同时获得未确定和检查。上面soumya的解决方案可能更有效。

回答by Majid Basirati

You can use this:

你可以使用这个:

var result = $('#your_tree').jstree('get_selected');

https://stackoverflow.com/a/22499278/1883345

https://stackoverflow.com/a/22499278/1883345

回答by Aamir Awan.

i did this to get id,parentid and text of selected checkbox. hopefully it will help someone :)

我这样做是为了获取所选复选框的 id、parentid 和文本。希望它会帮助某人:)

function myFunction(elmnt,clr){
         elmnt.style.color =clr;
         var selectedElmsinfo = [];

         var selectedElms = $('#SimpleJSTree').jstree("get_selected", true);
            $.each(selectedElms, function() {
                selectedElmsinfo.push(this.id,this.text,this.parent);

            });
             alert(selectedElmsinfo);
        }

回答by Malcon

//click button show nodes checked
$(document).on('click','#showme',function() {
    var checked_ids = [];
    var checked_ids_meta = [];
    $('#demo_0').jstree("get_checked",null,true).each(function(i, e){
        checked_ids.push($(this).data("param")); // json metadata
        checked_ids_meta.push($(this).attr("href")); // json attr
    });     
    console.log(checked_ids)
    console.log(checked_ids_meta)       
}); 

回答by Qiol Noon

var selectedElmsIds = [];
$("#jstree2").find("li").each(function(i, element) { //also includes the ones below 'undetermined' parent
  if ($(this).attr("aria-selected") == "true") {
    selectedElmsIds.push($(this).attr('id'));
  }
});
console.log(selectedElmsIds);

回答by t_plusplus

This I did:

这是我做的:

function getSelectedItems()
{
    var checked_ids = [];

    checkedNodes = $("#MyTree").jstree("get_checked", null, true); 

    for(var i = 0; i < checkedNodes.length; i++)
    {
        var id = $(checkedNodes[i].outerHTML)[0].id;

        checked_ids.push(id);
    }

     // Do whatever you want with the checked_ids 
}

This will give you an array of all selected node and their sub nodes and leafs; as well as single leafs selected under other nodes.

这将为您提供所有选定节点及其子节点和叶子的数组;以及在其他节点下选择的单个叶子。