【发布时间】:2019-12-31 06:53:47
【问题描述】:
我正在尝试使用如下所示的 jQuery 代码在文本框的按键上执行一些代码,但出现错误。你能帮我吗?我什至尝试过 keydown 但面临同样的问题。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<style>
textarea.ex1 { background-color: white; width: 610px; height: 310px; overflow: Scroll; }
#outer { width:100%; text-align: center;} .inner { display: inline-block; }
body { background-color: gray; }
</style>
</head>
<body>
<textarea id="screen" class="ex1"></textarea>
<input type="text" id="command" style="width: 480px; height=600px;"/>
<script>
$("#command").keydown(function(event){
alert('Before keycode validation cond');
var keycode = (event.keyCode ? event.keyCode : event.which);
alert('After keycode validation');
if(keycode == '13')
{
alert('ENTER key selected');
$.get('Servlet3', function(response) {
alert(response);
appendResponse(response);
});
appendCommand();
}
});
function appendCommand() {
alert('appending command');
document.getElementById('screen').textContent = document.getElementById('screen').textContent + document.getElementById('command').value;
}
function appendResponse(response) {
alert('appending response');
document.getElementById('screen').textContent = document.getElementById('screen').textContent + response;
}
</script>
【问题讨论】:
-
同时提供您的 HTML
-
我尝试使用 IE 11 浏览器测试您的代码,发现它运行良好。这是测试结果。 i.postimg.cc/pdvS1kF1/138.gif你能告诉我们你在这个测试中使用的是哪个版本的 iE 吗?另外,请告诉我们您在哪一行收到此错误?它可以帮助缩小问题范围。
-
我在 IE11 上运行上面的代码并从下面的行收到错误。 $("#command").keydown(function(event){
标签: jquery internet-explorer keypress keydown