简体   繁体   English

键盘功能不起作用

[英]keyup function not working

I am making a character counter in js but keyup event is not working ??? 我在js中创建字符计数器,但是keyup事件不起作用?

 $(document).ready(function() { function countingCharacter(element, maxCount) { var countLength = $('#' + element).val().length(); alert(countLength); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea onkeyup="countingCharacter("storyOutline",100)" onkeypress="countingCharacter("storyOutline",100);" onkeydown="countingCharacter("storyOutline",100)" id="storyOutline" rows="5" class="form-control" name="labinput7" placeholder="Story Outline" required></textarea> 

To avoid any issues related to the functions scope and keep JS code separated from HTML, I'd go with following event binding: 为了避免与函数范围有关的任何问题并使JS代码与HTML分开,我将使用以下事件绑定:

$('#storyOutline').on('keyUp', function() {
  var length = $(this).val().length();
  alert(length);
});

onkeyup="countingCharacter("storyOutline",100)"

"storyOutline" will close and open the quotes in onkeyup "storyOutline"将关闭并打开onkeyup的引号

You could use single quotes inside the double quotes: 您可以在双引号内使用单引号:

onkeyup="countingCharacter('storyOutline',100)"

You can get textarea length using event.currentTarget.textLength value 您可以使用event.currentTarget.textLength值获取textarea长度

 $(document).on("keyup", "#storyOutline", function (e) { console.log(e.currentTarget.textLength); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <textarea id="storyOutline" rows="5" class="form-control" name="labinput7" placeholder="Story Outline" required></textarea> 

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<script>
$(document).ready(function(){
    $("input").keyup(function(){
       alert("your pressed the key");
    });
});
</script>
</head>
<body>
Enter your name: <input type="text">
</body>
</html>

when you keyup on the text field, you'll find an alert message that you have keyed. 当您在文本字段上键入内容时,会发现已键入内容的警报消息。

Use single quotes to add arguments to the inline JavaScript functions, so: 使用单引号将参数添加到嵌入式JavaScript函数,因此:

<textarea onkeyup="countingCharacter('storyOutline')" ... ></textarea>

Instead of 代替

<textarea onkeyup="countingCharacter("storyOutline")" ...></textarea>

But you don't actually need those parameters (you can keep maxCount inside the function's scope). 但是实际上并不需要这些参数 (可以将maxCount保留在函数的作用域内)。 Then access the target (input box element) with event.target . 然后使用event.target访问目标(输入框元素)。 Select its input value and its length. 选择其输入值和长度。

Here's a working code: 这是一个工作代码:

 function countingCharacter(maxCount) { console.log($(event.target).val().length); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea onkeyup="countingCharacter(100)" onkeypress="countingCharacter(100);" onkeydown="countingCharacter(100)" id="storyOutline" rows="5" class="form-control" name="labinput7" placeholder="Story Outline" required></textarea> 

You shouldn't use alert for debugging, use console.log instead. 您不应将alert用于调试,而应使用console.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM