最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting value from input control using jQuery - Stack Overflow

programmeradmin1浏览0评论

I am using the teleriks treeview control (asp 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 lis 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, i.e. if Node 1.1.2 is clicked return 2, Node 1.1 return 1 and node 1 returns 0

I am using the teleriks treeview control (asp 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 lis 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, i.e. if Node 1.1.2 is clicked return 2, Node 1.1 return 1 and node 1 returns 0

Share Improve this question edited Aug 9, 2019 at 3:33 Glorfindel 22.7k13 gold badges90 silver badges119 bronze badges asked Feb 28, 2011 at 5:59 baronbaron 11.2k21 gold badges58 silver badges88 bronze badges 2
  • I'm having a hard time understanding what exactly you want to return. You say you want the appropriate ID, but what ID specifically are you looking to return. – Seth Commented Feb 28, 2011 at 22:31
  • I edited in a solution to your follow-up question in my answer below. – Alconja Commented Mar 1, 2011 at 21:56
Add a ment  | 

3 Answers 3

Reset to default 3

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 lis 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();
});
发布评论

评论列表(0)

  1. 暂无评论