jquery - 在textarea jquery中单击输入时发送表单
问题描述
我有这个 jquery
$(document).ready(function () {
$("#submitmsg").click(function () {
var clientmsg = $("#usermsg").val();
$.post("post.php", { text: clientmsg });
$("#usermsg").val("");
return false;
});
});
这是我的html
<form name="message" action="" id="message">
<textarea name="usermsg" id="usermsg"></textarea>
<div class="submit"><input name="submitmsg" type="submit" id="submitmsg" value="Send"></div>
</form>
因为此时只能通过按下按钮发送表格。但是,我想通过单击文本区域内的输入来发送它。但我还需要能够通过按 shift + enter 来换行。
我怎样才能在 jquery 中做到这一点?我试过了:
$("#usermsg").keypress(function (e) {
if (e.which == 13) {
var clientmsg = $("#usermsg").val();
$.post("post.php", { text: clientmsg });
$("#usermsg").val("");
return false;
}
});
但我不能让它在换档时断线。
谢谢你。
解决方案
检查shiftKey
事件对象的属性
$("#usermsg").keypress(function(e) {
if (e.which == 13) {
console.log(e.shiftKey ? 'Shift & Enter' : 'Enter only')
if (!e.shiftKey) {
// do ajax here
return false
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="message" action="" id="message">
<textarea name="usermsg" id="usermsg"></textarea>
<div class="submit"><input name="submitmsg" type="submit" id="submitmsg" value="Send"></div>
</form>
推荐阅读
- django - Django 'ImportError: 无法导入名称 url'
- json - PySpark - 获取字典列表并将其键/值转换为列
- c++ - 如何计算字母在随机字母字符串中出现的次数
- reactjs - 在嵌套组件方面难以理解 componentDidMount() 方法
- c++ - 如何将 UTF8 std::string 转换为 NSString?
- php - 如何填充下拉列表?
- sql-server-2017 - 如何修复 SQL Server 中的这个 datediff 错误?
- python-3.x - 从录制的音频中获取频谱图,错误:“ParameterError:音频缓冲区并非到处都是有限的”
- excel - Excel VBA:如何将记录从一个工作表附加到另一个工作表的最后一行
- python - Selenium Chromedriver 错误代码:3221225477