如何从JQuery动态创建的隐藏字段中获取价值? [英] How to get value from dynamically created hidden field in JQuery?

查看:55
本文介绍了如何从JQuery动态创建的隐藏字段中获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用例中,我试图从JQuery中动态生成的隐藏字段中获取值。当我单击该迭代的按钮时,我应该得到隐藏字段的值属于该迭代。但我无法得到它。它将值设为'undefined'

In my use case, I am trying to get value from dynamically generated hidden field in JQuery. When I click the button for that iteration I should get the value for the hidden field belongs to that iteration. But I am not able to get it. It is giving the value as 'undefined'

HTML:

<div class="comment-list-new" style= "max-height: 660px !important;overflow-y: scroll;">
    <h5>Discussion Board</h5>
    <ol>
        {{  if .ViewData.Questions }}
        {{ range .ViewData.Questions }}
            <li>
                <p id="question_id" class="question_id_val" hidden>{{.QuestionId}}</p>
                <div class="q-comment">
                    <div class="qanda questiondiv" id="questionarea" name="questionarea">
                        <div>
                            <div id="topic" class="upvote pull-left">
                                <a class="upvote"></a>
                                <span class="count">3</span>
                                <a class="downvote"></a>
                            </div>
                            <div >
                                <div class="qanda-info">
                                    <h6><p id="quest_title">{{.QuestionTitle}}</p></h6>
                                </div>
                                <p id="quest_text">{{.QuestionText}}</p>
                            </div>
                        </div >
                        <div class="qanda-info">
                        <div class="user-info">
                            <img src="/resources/img/team-small-2.png" />
                        </div>
                        <h6>{{.UserId}}</h6>
                        <span class="date alt-font sub">{{.DateCreated}}</span>
                        <a id="answertext" name ="answertext" type="submit" class="link-text answerbutton">Answer</a>
                    </div>
                    </div>
                </div>
            </li><!--end of individual question-->
         {{ end }}
        {{ end }}
    </ol>
</div><!--end of comments list-->

JS:

$('.questiondiv').on('click', '.submitanswerbutton', function() {
    console.log("In submit button");
    var question_id = $(this).closest('.question_id_val').val();
    var answer_text = $('.answertext_val').val();
    console.log(question_id);
    console.log(answer_text);
    $.getJSON("/submitanswer?question_id="+question_id+"&answer="+answer_text, function(data) {
            console.log("answer Response"+data);
            newQuestion = "<li><div class='q-comment'><div class='qanda' id='questionarea' name='questionarea'><div><div id='topic' class='upvote pull-left'><a class='upvote'></a><span class='count'>0</span><a class='downvote'></a></div><div ><div class='qanda-info'><h6><p id='quest_title'>"+title+"</p></h6></div><p id='quest_text'>"+desc+"</p></div></div ><div class='qanda-info'><div class='user-info'><img src='/resources/img/team-small-2.png' /></div><h6>Chip Mayer</h6><span class='date alt-font sub'>September 17 2014</span><a id='answertext' name ='answertext' type='submit' class='link-text'>Answer</a></div></div></div></li>";
            $('ol').append(newQuestion);
    });
});

在上面的代码中,我试图获取隐藏字段的值 question_id_val
有谁可以帮我这个?

In the above code I am trying to get the value for the hidden field question_id_val. Could anyone help me with this?

推荐答案

使用 nearest()获取对外部容器(li)的引用,然后使用 find()方法获取隐藏字段。

Use closest() to get a reference to the outer container (li) and then use find() method to get the hidden field.

 var question_id = $(this).closest('li').find('.question_id_val').val();

val()方法适用于通常输入表单字段(文本框,隐藏字段等..)。因此,您需要确保您的元素是页面中的有效表单字段。

val() method works for usually input form fields(textbox,hidden fields etc..) .So you need to make sure your element is a valid form field in your page.

<input type="hidden" id="question_id" class="question_id_val" />

或者如果你想保持你的p标签,请使用 html() text()获取p标签内容的方法。

Or if you want to keep your p tag as it is, Use the html() or text() method to get the content of the p tag.

var question_id = $(this).closest('li').find('.question_id_val').text();

请记住,这些方法也会返回所有子内容的text / html。所以一定要明智地使用它。

Remember, these method returns the text/html of all child content as well. So make sure to use it wisely.

这篇关于如何从JQuery动态创建的隐藏字段中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆