get checked values for jsTree - submit with form post

前端 未结 11 1641
南笙
南笙 2020-12-14 02:40

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

Thank you!

相关标签:
11条回答
  • 2020-12-14 02:58

    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);

    0 讨论(0)
  • Have you got your answer ? If not, here is one that appears in the jstree google groups

        function submitMe(){ 
            var checked_ids = []; 
            $("#server_tree").jstree("get_checked",null,true).each 
                (function () { 
                    checked_ids.push(this.id); 
                }); 
               doStuff(checked_ids); 
    
    
    0 讨论(0)
  • 2020-12-14 03:00

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

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

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

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

    var selectedElmsIds = [];
    var selectedElms = $('#tree').jstree("get_selected", true);
    $.each(selectedElms, function() {
        selectedElmsIds.push(this.id);
    });
    
    0 讨论(0)
  • 2020-12-14 03:00

    With jQuery you can simply do:

    $('.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.

    0 讨论(0)
  • 2020-12-14 03:08
    $(document).ready(function(){
    var jsfields = $('#myTree').jstree('get_selected');
    $('.jsfields').val(JSON.stringify([jsfields]));
    })
    
    <input type="hidden" class="jsfields" value=""/>
    

    Change the value of $('#myTree') to you respective tree, this is best working for me in ajax call. slight modification may be needed to populate input field of simple form.

    0 讨论(0)
  • 2020-12-14 03:11
    //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)       
    }); 
    
    0 讨论(0)
提交回复
热议问题