替换div元素在JQuery中不起作用?

时间:2016-03-10 06:59:11

标签: javascript jquery html replacewith

在我的用例中,我有问题标题和问题文本标签。当我按下编辑按钮时,我想用带有问题标题和问题文本的文本框替换标签。同时提交和取消按钮进行编辑。在此编辑中如果我按下取消,我应该替换原始div内容。以下代码正在工作,直到取消。当我按取消时,它没有正确替换原始内容。编辑按钮也被禁用。我没有手动禁用任何编辑按钮。

JQuery代码:

//global variable for question edits
var originalQuestion = "";

//Enables and disables the question elements 
//for all questions
$('.questiondiv').on('click', '.edit_question_button', function() {
    console.log("In submit edit question text area");
    var pageURL = $('#page_url').text();
    var loggedUser = $('#logged_user').text();
    if (!loggedUser) {
        console.log("In answerbutton redirect");
        window.location.href = "/login/?redirect="+pageURL;
    } else {
        console.log("question edit");
        title_val       = $(this).closest('.question-inner').find('.quest_title_val').text();
        question_val    = $(this).closest('.question-inner').find('.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='81' 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>";
        originalQuestion = $(this).closest('.questiondiv').html()
        $(this).closest('.question-inner').replaceWith(newdiv);
    }
});


//updates question
//question ID may have to added as 
//hidden element in the html
$('.questiondiv').on('click', '.updatequestion', function() {
    console.log("In update question button");
    var question_id = $(this).closest('li').find('.question_id_val').val();
    var quest_title = $(this).closest('.updated_question').find('.quest_title_val').text();
    var quest_val   = $(this).closest('.updated_question').find('.quest_text_val').text();
    console.log(quest_title);
    console.log(quest_val);

    $.getJSON("/question?question_id="+question_id+"&question_title"+quest_title+"&question_val="+quest_val, function(data) {
        console.log("update question Response "+data);
        $(this).closest('.updated_question').replaceWith(originalQuestion);
    });
    //$(this).closest('.answersectiondiv').remove();
});


//cancel updatequestion elements
$('.questiondiv').on('click', '.cancelupdatequestion', function() {
    console.log("In cancel update question area");
    //console.log("Original question div tag ", originalQuestion)
    $(this).closest('.question-inner').replaceWith(originalQuestion); //Again use this to remove only parent container div
    //$(this).closest('.questiondiv').find('.edit_question_button').prop( 'disabled' , true );
});

HTML code:

<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="deleted" ">delete</a>]<br>
            <a id="answertext" name ="answertext" type="submit" class="link-text answerbutton">Answer</a>
        </div>
    </div>
</div>

有人可以帮我吗?感谢

0 个答案:

没有答案