javascript - Jquery在按Enter键时获取textarea值
问题描述
我试图在按下回车键时获取文本区域的值,但获取值未定义。
jQuery(".comment-form .comments-text").on('keypress', function (e) {
if(e.which === 13 && !e.shiftKey) {
e.preventDefault();
var text = jQuery(this).value;
console.log(e);
}
});
on keypress 工作正常,但无法获取我输入的文本。
解决方案
此代码应该可以工作:
HTML
<form class="comment-form">
<textarea class="comments-text"></textarea>
</form>
jQuery
$(".comment-form .comments-text").on('keypress', function(e) {
if (e.which === 13 && !e.shiftKey) {
e.preventDefault();
var text = e.target.value;
console.log('text', text);
}
});
推荐阅读
- android - 图像不是类型 imageprovider 动态的子类型
- go - 如何从 Telnet 会话中读取数据
- c# - 用剃须刀页面填充html表格中的特定单元格
- ubuntu - Ubuntu 上的 AWS + Remmina - Remmina 进入循环尝试重新连接
- javascript - 如何在 ReactJS 中预先检查一些复选框
- javascript - 表单在提交发布请求时被清除
- python - Python将纯文本显示为html
- julia - Julia MethodError: 没有方法匹配 (::Dense{typeof(logistic),CuArray{Float32,2,Nothing},CuArray{Float32,1,Nothing}})(::Float32)
- python - 如何从 python 3.8.0 IDLE shell 运行 python 程序
- sqlite - Flutter中的Sqflite数据库异常类型