How to get textbox value from replaced div content using replacewith?

霸气de小男生 提交于 2019-12-12 03:24:20

问题


In my use case, I will replace two labels with text boxes dynamically from javascript. And I want to get the updated value from the text box. But it is giving the original content from the label not the updated value in textbox.

In original div:

  • label1
  • lable2
  • EditButton

After I press the EditButton,

  • Text1
  • Text2
  • UpdateButton

I will edit the content in Tex1, Text2 and press the UpdateButton I am expecting the edited value from Text1, Text2 in javascript but I am getting the original value from label1, label2.

My Javascript code is here,

var originalQuestion = "";
$('.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");
        var title_val       = $(this).closest('.question-inner').find('.quest_title_val').text();
        var question_val    = $(this).closest('.question-inner').find('.quest_text_val').text();
        console.log("Question title value ", title_val);
        console.log("Question value ", question_val);

        var newdiv = "<div class='question-inner'><div class='form-input updated_question'><input name='question_title_val' id='question_title_val' type='text' size='81' value='"+title_val+"' /></div> <br /> <div class='form-input'><textarea name='question_text_val' id='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></div>";
        originalQuestion = $(this).closest('.question-inner').html();
        $(this).closest('.question-inner').replaceWith(newdiv);
    }
});

$('.questiondiv').on('click', '.updatequestion', function() {
    console.log("In update question button");
    var question_id = $(this).closest('li').find('.question_id_val').val();
    quest_inner = $(this).closest('.question-inner').html();
    console.log("In update question button :: quest inner content ", quest_inner);
    var quest_title = $(this).closest('.question-inner').find('#question_title_val').text();
    var quest_val   = $(this).closest('.question-inner').find('#question_text_val').text();
    $.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);
    });
});

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

Could anyone help me on this?

来源:https://stackoverflow.com/questions/35965155/how-to-get-textbox-value-from-replaced-div-content-using-replacewith

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!