在我的用例中,我将使用javascript动态替换带有文本框的两个标签。我想从文本框中获取更新的值。但是它提供了标签中的原始内容而不是文本框中的更新值。
原来的div:
按下EditButton后,
我将编辑Tex1,Text2中的内容并按下UpdateButton我期望javascript中Text1,Text2的编辑值,但我从label1,label2获取原始值。
我的Javascript代码在这里,
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);
});
});
我的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>
有人可以帮我吗?