简体   繁体   English

按键事件如何工作?

[英]How does the keydown event work?

I want that if my input text value is not empty then show my .removetext div but it doesn't work properly. 我想要如果输入的文本值不为空,则显示我的.removetext div,但无法正常工作。 If you type a something after second character my .removetext is being hidden but if my input is not empty don't hide my .removetext class. 如果您在第二个字符后输入内容,则我的.removetext隐藏,但是如果我的输入不为空,请不要隐藏我的.removetext类。

Where is my mistake? 我的错误在哪里? I guess I don't understand the keydown event 我想我不了解按键事件

 $(document).ready(function() { $('.search-hotels').on('input', function() { var val = $.trim(this.value); if (!val == "") { $(".removetext").show(val.length > 0, function() { var clear = $(this); clear.on('click', function() { alert('hey'); }) }); } else { $(".removetext").hide(); } }); }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

You can achieve what you want by using keyup instead of keydown and changing your if statement to the following: 通过使用keyup而不是keydown并将if语句更改为以下内容,可以实现所需的功能:

if ($(this).val());

Using keydown means that it will not use the .val that was input but will check the .val before the input is updated because the .val will be updated after the keyup event. 使用keydown意味着它将不使用输入的.val ,而是将在更新输入之前检查.val ,因为.val将在keyup事件之后更新。

An even better approach would be to use the input event as follows: 更好的方法是使用input事件 ,如下所示:

 $(document).ready(function() { $('.search-hotels').on('input', function() { if ($(this).val()) { $(".removetext").show(function() { console.log($(this).attr('class')); }); } else { $(".removetext").hide(); } }) }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

Try input - it handles paste too: 尝试输入-它也处理粘贴:

 $(function() { $('.search-hotels').on('input', function() { var val = $.trim(this.value); $(".removetext").toggle(val.length>0); // test anything not blank }); // the event handler needs to be HERE and not inside the inout handler $(".removetext").on('click', function() { alert('hey'); }); }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels" value=""> <div class="removetext">&times;</div> </div> 

If you need a callback you do need show and hide 如果需要回调,则需要显示和隐藏

 $(function() { $('.search-hotels').on('input', function() { var show = $.trim(this.value).length>0; if (show) { $(".removetext").show("slow",function() { console.log("showing"); }); } else { $(".removetext").hide("slow",function() { console.log("hiding"); }); } }); }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels" value=""> <div class="removetext">&times;</div> </div> 

Keydown event is fired before a character is typed, hence you should use keyup event which is fired after the character is typed. 在键入字符之前会触发Keydown事件,因此您应该使用在键入字符后触发的keyup事件。

Also, !$(this).val() is different than $(this).val()=="" 此外, !$(this).val()$(this).val()==""

Use if($(this).val()=="") 使用if($(this).val()=="")

You should use keyup. 您应该使用keyup。 Key down registers the key pressed but it hasn't populated the input yet. 按下按键会记录按下的按键,但尚未填充输入。 You can either go through the keydown event properties and use the property "key" (if i remember correctly) to get the value entered during the keydown, but if you want to grab the text in the input, you'll need "keyup" 您可以浏览keydown事件属性,并使用属性“ key”(如果我没记错的话)来获取在keydown期间输入的值,但是如果要获取输入中的文本,则需要“ keyup”

you can use keyup here better and checking for $(this).val().length would be better. 您可以在此处更好地使用keyup并检查$(this).val().length会更好。

 $(document).ready(function() { $('.search-hotels').on('keyup', function() { if ($(this).val().length > 0) { $(".removetext").show(function() { console.log($(this).attr('class')); }); } else { $(".removetext").hide(); } }) }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

It should be if ($(this).val()) otherwise your checking if it's empty. 应该是if ($(this).val())否则检查是否为空。 Also, use keyup as keydown is capturing your val() before it actually has a value. 另外,将keyup用作keydown是在val()实际具有值之前捕获它。

 $(document).ready(function() { $('.search-hotels').on('keyup', function() { if ($(this).val()) { $(".removetext").show(function() { console.log($(this).attr('class')); }); } else { $(".removetext").hide(); } }) }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

keydown event fires when the button is pressed, at that moment your value isn't updated, try using keyup event fires when the button is released and the input value is updated... 当按下按钮时会触发keydown事件,此时您的值不会更新,请尝试在释放按钮并更新输入值时使用keyup事件。

 $(document).ready(function() { $('.search-hotels').on('keyup', function() { console.log(this,$(this).val()); if ($(this).val()) { $(".removetext").show(function() { console.log($(this).attr('class')); }); } else { $(".removetext").hide(); } }) }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

You should not use keydown event because it is one keypress "behind" of what's written into input value . 您不应该使用keydown事件,因为它是输入value写入内容之后的一个按键。

Ideally you should use oninput (not even keyup as suggested by everyone, but @mplungjan who was faster :). 理想情况下,您应该使用oninput (甚至不是每个人都建议的keyup,而是@mplungjan,它速度更快:)。 oninput will also allow you to properly track copy/past events unlike onkeyup. 与onkeyup不同, oninput还可以让您正确跟踪复制/ 粘贴事件。

In addition, read input value by simply this.value but remember to trim it also to get rid of whitespaces. 另外,仅通过this.value读取输入值,但要记住也要对其进行修剪以消除空格。

 $(document).ready(function() { $('.search-hotels').on('input', function() { if (this.value.trim()) { $(".removetext").show(); } else { $(".removetext").hide(); } }) }); 
 .main { position: relative; width: 40%; } .search-hotels { width: 100%; padding: 15px; border: 3px solid #ccc; } .removetext { position: absolute; right: 0; top: 25%; font-size: 23px; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <input type="text" class="search-hotels"> <div class="removetext">&times;</div> </div> 

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

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