大家好,我正在尝试使用 jquery keydown 函数和以下代码进行预输入以发送评论。但是我总是收到“请写下您的评论”错误。我在这里缺少什么任何人都可以在这方面帮助我?
$('.sendcomment').bind('keydown', function(e) {
if(e.keyCode==13) {
var ID = $(this).attr("id");
var OID = $(this).attr("rel");
var URL = $.base_url + 'sendcomment_ajax.php';
var comment = $(this).parents('.user-post').find("#commentid" + ID).val();
var dataString = 'comment=' + comment + '&msg_id=' + OID;
if ($.trim(comment).length == 0) {
swal({
title: "Plese write your comment!",
text: "",
timer: 5000
});
} else {
$.ajax({
type: "POST",
url: URL,
data: dataString,
cache: false,
success: function(html) {
$("#commentload" + ID).append(html);
$("#commentid" + ID).val('').css("height", "35px").focus();
}
});
}
return false;
}
});
HTML 文本区域
<div class="comments-area" id="commentload<?php echo $msgid;?>">
comments will be come here
</div>
<div class="user-post" id="postbody<?php echo $msgid;?>">
<textarea class="sendcomment" name="comment" id="commentid<?php echo $msgid;?>"></textarea>
<input type="submit" class="send-comment comment_button commentopen" id="<?php echo $msgid;?>" rel="<?php echo $msgid;?>" value="SEND" />
</div>
最佳答案
根据您的信息,我认为您的 comment
变量的选择器有问题,因为您的测试总是告诉您您的评论值的长度为 0。
此外,既然您似乎有评论 ID,为什么不直接使用这个更简单的选择器:$('#commentid' + ID)
?
关于javascript - Jquery keydown 不会丢失任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521317/