views:

189

answers:

2

I am just starting out with JQuery and Javascript, and adapting a nested set of checkbox inputs to make use of jstree checkbox behaviours. After realising that each item had to be an anchor tag to be recognised by the jstree code, I am now unsure how to generate values from the items for a POST form submission. Previously they were checkbox inputs, and I would like to be able to keep them this way so that the form would still function if Javascript were turned off. wrapping inputs in the anchor tag resulted in two checkboxes for each item, one styled and behaviourally controlled by JStree, the other ignored. I assume this isn't the direction to go.

Any advice on how to achieve this would be gratefully received.

I am booting JStree with the following config code -

$(document).ready(function()
{
    $('.nested-category').jstree({ 
        ui: { theme_name : "checkbox" },
        plugins : ["checkbox", "themes", "html_data", "ui" ]
    });
}
);

Here is a simplified version of the form which the JStree code effects, but which can't submit values.

<form method="post" action="">

<div class="nested-category">

<ul class="">
    <li><a href="#">group1</a>
        <ul>
            <li><a href="#">subgroup1</a>
                <ul>
                    <li><a href="#">item1</a></li>
                    <li><a href="#">item2</a></li>
                    <li><a href="#">item3</a></li>
                </ul>
            </li>
            <li><a href="#">subgroup2</a>
                <ul>
                    <li><a href="#">item4</a></li>
                    <li><a href="#">item5</a></li>
                    <li><a href="#">item6</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">group2</a>
        <ul>
            <li><a href="#">subgroup3</a>
                <ul>
                    <li><a href="#">item7</a></li>
                    <li><a href="#">item8</a></li>
                    <li><a href="#">item9</a></li>
                </ul>
            </li>
            <li><a href="#">subgroup4</a>
                <ul>
                    <li><a href="#">item10</a></li>
                    <li><a href="#">item11</a></li>
                    <li><a href="#">item12</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
</div>

<input class="form-submit" type="submit" name="submit" value="submit">
</form>
A: 

personally, i would use AJAX. $.post

give the anchors an ID, and in jsTree i'm sure there is a method that gets all selected ID's

Stefanvds
Yes, I'm starting to think that now I've gone down the JStree road, it's not possible to allow this form to function as a normal HTML form if Javascript is turned off.
bulkhead
well, that's pretty obvious :) question is do you really need this form, do you have users without javascript? not much websites care about ppl without JS these days...
Stefanvds
There are certain obligations to accessibility standards for this project, so things should function without JS. If the jstree code could be adapted to make <input> tags the nodes rather than anchors, then the problem would be solved. The form is dynamically constructed with PHP, so I'm not keen on re-jigging it completely to all be controlled by JS. I was hoping I could adapt JStree to work with <input> tags.
bulkhead
It's occured to me that I could have the input tag inside the anchor tag, and control it's checked/unchecked state by adding a few lines to the control for that state in the Jquery plugin. The input could then be hidden, but still pass values on submission. The input checkbox won't be directly controllable by the user unless Javascript is off, which should be perfect.Now I just need to work out the Jquery syntax for "if this node has a child which is an input within the a tag, change checked="checked"/"unchecked" to add to the pre-exisiting changes of state.
bulkhead