简体   繁体   中英

Getting value from input control using jQuery

I am using the teleriks treeview control (asp.net mvc extensions), where I may have up to three children nodes, like so (drumroll...... awesome diagram below):

it has its own formatting, looking a bit like this:

<%= 
    Html.Telerik().TreeView()
        .Name("TreeView")
        .BindTo(Model, mappings =>
                           {
                               mappings.For<Node1>(binding => binding
                                                                     .ItemDataBound((item, Node1) =>
                                                                                        {
                                                                                            item.Text = Node1.Property1;
                                                                                            item.Value = Node1.ID.ToString();
                                                                                        })
                                                                     .Children(Node1 => Node1.AssocProperty));

                               mappings.For<Node2>(binding => binding
                                                                         .ItemDataBound((item, Node2) =>
                                                                                            {
                                                                                                item.Text = Node2.Property1;
                                                                                                item.Value = Node2.ID.ToString();
                                                                                            })
                                                                         .Children(Node2 => Node2.AssocProperty));

                               mappings.For<Node3>(binding => binding
                                                                  .ItemDataBound((item, Node3) =>
                                                                                     {
                                                                                         item.Text = Node3.Property1;
                                                                                         item.Value = Node3.ID.ToString();
                                                                                     }));
                           })
 %> 

which causes it to render like this. I find it unsual that when I set the value it is rendered in a hidden input ? But anyway:...

<li class="t-item">
<div class="t-mid">
    <span class="t-icon t-plus"></span>
    <span class="t-in">Node 1</span>
    <input class="t-input" name="itemValue" type="hidden" value="6" /></div>

        <ul class="t-group" style="display:none">
            <li class="t-item t-last">
                <div class="t-top t-bot">
                    <span class="t-icon t-plus"></span>
                    <span class="t-in">Node 1.1</span>
                    <input class="t-input" name="itemValue" type="hidden" value="207" />
                </div>

                    <ul class="t-group" style="display:none">
                        <li class="t-item">
                            <div class="t-top">
                                <span class="t-in">Node 1.1.1</span>
                                <input class="t-input" name="itemValue" type="hidden" value="1452" />
                            </div>
                        </li>

                        <li class="t-item t-last">
                            <div class="t-bot">
                                <span class="t-in">Node 1.1.2</span>
                                <input class="t-input" name="itemValue" type="hidden" value="1453" />
                            </div>
                        </li>
                    </ul>
            </li>
        </ul>

What I am doing is updating a div after the user clicks on a certain node. But when the user clicks on a node, I want to send the ID not the Node text property. Which means I have to get it out of the value in these type lines <input class="t-input" name="itemValue" type="hidden" value="1453" /> , but it can be nested differently each time, so the existing code I ahve doesn't ALWAYS work:

<script type="text/javascript">

    function TreeView_onSelect(e) {
    //`this` is the DOM element of the treeview
        var treeview = $(this).data('tTreeView');

        var nodeElement = e.item;
    var id = e.item.children[0].children[2].value;


...

</script>

So based on that, what is a better way to get the appropriate id each time with javascript/jquery?

edit :

Sorry to clarify a few things

1) Yes, I am handling clicks to the li s of the tree & want to find the value of the nested hidden input field. As you can see, from the telerik code, setting item.Value = Node2.ID.ToString(); caused it to render in a hidden input field.

I am responding to clicks anywhere in the tree, therefore I cannot use my existing code, which relied on a set relationship (it would work for first nodes (Node 1) not for anything nested below)

What I want is, whenever there is something like this, representing a node, which is then clicked:

<li class="t-item t-last">
                            <div class="t-bot">
                                <span class="t-in">Node 1.1.2</span>
                                <input class="t-input" name="itemValue" type="hidden" value="1453" />
                            </div>
                        </li>

I want the ID value out of the input, in this case 1453 .

Hope this now makes a lot more sense.

if possible would love to extend this to also store in a variable how nested the element that is clicked is, ie if Node 1.1.2 is clicked return 2, Node 1.1 return 1 and node 1 returns 0

It's a little unclear what you're asking, but based on your snippet of JavaScript, I'm guessing that you're handling clicks to the li s of the tree & want to find the value of the nested hidden field? If so, you want something like this:

function TreeView_onSelect(e) {
    var id = $(e.item).find(".t-input:first").val();
}

Edit : In answer to your follow-up question, you should be able to get the tree depth with the following:

var depth = $(e.item).parents(".t-item").length;

In jQuery you can return any form element value using .val();

$(this).val(); // would return value of the 'this' element.

I'm not sure why you are using the same hidden input field name "itemValue", but if you can give a little more clarity about what you are asking I'm sure it's not too difficult.

$('.t-input').live('change',function(){
   var ID_in_question=$(this).val();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM