如何从JQuery获取最接近的<p>值?

时间:2016-03-10 06:01:12

标签: jquery html paragraph closest

在我的用例中,我试图在JQuery中获得最接近的段落值。在html中,我在div元素列表中进行迭代,并尝试使用任何迭代中的类名来获取段值[选择]。

这是我的Html代码:

<div class="qanda questiondiv" id="questionarea" name="questionarea">
    <div class="question-inner">
        <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 class="quest_title_val">{{.QuestionTitle}}</p></h6>
                </div>
                <p class="quest_text_val">{{.QuestionText}}</p>
            </div>
        </div >
        <div class="qanda-info">
            <div class="user-info">
                <a href="/userprofile/{{.UserId}}" ><img src="http://tinygraphs.com/spaceinvaders/Dany?theme=frogideas&numcolors=4&size=220&fmt=svg"/>
                </a>
            </div>
            <h6>{{.UserId}}</h6>
            <span class="date alt-font sub">{{.DateCreated}}</span>
            [<a class="edit_question edit_question_button" >edit</a>|<a class="delete_question_button" id="delted" href="/edithowti/">delete</a>]<br>
            <a id="answertext" name ="answertext" type="submit" class="link-text answerbutton">Answer</a>
        </div>
    </div>
</div>

我的JQuery代码:

var originalQuestion = "";

//Enables and disables the question elements 
//for all questions
$('.questiondiv').on('click', '.edit_question', function(e) {
    console.log("In submit edit question text area");
    console.log("question edit");
    e.preventDefault(); 
    //title_val = $(this).closest('p').text('#quest_title_val');
    title_val       = $(this).prev('p').find('.quest_title_val').val();
    question_val    = $(this).closest('.quest_text_val').text();
    console.log("question edit logged in 2");
    console.log("Question title value ", title_val);
    console.log("Question value ", question_val);

    newdiv = "<div class='form-input updated_question'><input name='question_title_val' class='question_title_val' type='text' size='80' value='"+title_val+"' /></div> <br /> <div class='form-input'><textarea name='question_text_val' class='question_text_val' rows='8' cols='80'>"+question_val+"</textarea><br /><div class='form-input'><a type='Submit' id='updatequestion'  name='updatequestion' class='btn btn-primary updatequestion'>Update Question</a><a type='Cancel' id='cancelupdatequestion' name='cancelupdatequestion' class='btn btn-primary cancelupdatequestion'>Cancel</a></div></div>";
    $('.edit_question_button').prop( 'disabled' , true );
    oiginalQuestion = $(this).closest('.questiondiv').innerHTML;
    console.log("Cached div original ", originalQuestion)
    $(this).closest('.question-inner').replaceWith(newdiv);
});

我尝试了next(),prev(),nearest()。但没有运气。有人可以帮我吗?我对JQuery很新。

1 个答案:

答案 0 :(得分:0)

names<-