这是我网站的评论脚本,但 jquery/javascript 代码的某些部分似乎无法正常工作。
当编辑评论而不是将其发布到 php 时,我制作了一个小通知窗口来测试它是否实际上正在更改传递的值。
Onclick 将获取当前评论并创建一个包含该评论的文本区域。
现在,如果用户开始更改文本并点击“编辑”按钮,即使文本完全不同,小测试通知仍会显示原始评论。
请帮忙。
这是代码
$("a.edit").click(function(){
var eid = $(this).data("id");
var econtent = $("li[data-id='" + eid + "'] .comment-cont").text();
$("li[data-id='" + eid + "'] .comment-cont").html('<div class="edit-error"></div><form><textarea class="edit-comment">'+econtent+'</textarea></form><a href="javascript:;" onclick="notify('+econtent+')" class="editcomment button_yellow">Edit</a>');
$(".edit-comment").keyup(function(){
var content = $('.edit-comment').val();
if (content.length < 3){
$(".edit-error").show();
$('.edit-error').html('Content can not be shorter then 3 characters');
$("a.editcomment").hide();
}else if(content.length < 300){
$(".edit-error").hide();
$('.edit-error').html('')
$("a.editcomment").show();
}else{
$(".edit-error").show();
$('.edit-error').html('Content can not be longer then 300 characters');
$("a.editcomment").hide();
}
});
$("a.editcomment").click(function(){
var edited = $('.edit-comment').text();
notify(edited);
});
});
因此,如果用户单击编辑按钮,他/她的评论将进入带有“编辑”按钮的文本区域。
PS:功能
notify() = small notification box.
var eid = comment id.
var econtent = the original content
var edited = the edited content ( that still is the original content ).
最佳答案
我认为您只需将 $('.edit-comment').text();
更改为 $('.edit-comment').val();
$("a.editcomment").click(function(){
var edited = $('.edit-comment').val();
notify(edited);
});
看来val() is the preferred way of getting text from a textarea .
关于javascript - jQUERY 文本区域在键入时不会更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20205093/