如何从JQuery中动态创建的隐藏字段中获取价值?

时间:2015-12-19 01:12:05

标签: javascript jquery html hidden

在我的用例中,我试图从JQuery中动态生成的隐藏字段中获取值。当我单击该迭代的按钮时,我应该得到隐藏字段的值属于该迭代。但我无法得到它。它将值赋予'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的值。 有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

如果你知道一个css选择器idclass我不明白为什么你不能做这样的事情:

var question_id = $('#question_id').text();

var question_id = $('.question_id_val').text();

答案 1 :(得分:0)

val()应主要用于select,textarea和input元素。

获取内部文本时使用html()或text()

var question_id = $(this).closest('.question_id_val').html();

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