admin管理员组文章数量:1426794
If I press (for the first time) a character, than the alert prints an empty value.
How is possible? I don't understand.
$('#search-vulc').on('keydown', function() {
var textinsert = ($(this).val()).toLowerCase();
alert(textinsert);
});
Please tell me how I can print it with the first time, when the character is pressed.
Here is there also a jsfiddle example:
/
If I press (for the first time) a character, than the alert prints an empty value.
How is possible? I don't understand.
$('#search-vulc').on('keydown', function() {
var textinsert = ($(this).val()).toLowerCase();
alert(textinsert);
});
Please tell me how I can print it with the first time, when the character is pressed.
Here is there also a jsfiddle example:
https://jsfiddle/06xg4c78/1/
Share Improve this question edited Jun 5, 2018 at 7:51 Michel 4,1574 gold badges37 silver badges56 bronze badges asked Jun 13, 2016 at 13:16 BorjaBorja 3,5797 gold badges38 silver badges75 bronze badges 7-
8
keydown event occures before the value is updating, use
keyup
orinput
event instead – Pranav C Balan Commented Jun 13, 2016 at 13:17 - Use console.log to debug, not alert – j08691 Commented Jun 13, 2016 at 13:18
- @PranavCBalan how kind of form event ? maybe change() ? – Borja Commented Jun 13, 2016 at 13:19
- @Borja : which fires when you leave focus in case of text field – Pranav C Balan Commented Jun 13, 2016 at 13:20
- 1 @PranavCBalan ok thanks a lot for the suggest ;) – Borja Commented Jun 13, 2016 at 13:21
3 Answers
Reset to default 3Use keuyp event:
This is because keypress events
are fired before the new character is added to the value of the element (so the first keypress event
is fired before the first character is added, while the value is still empty). You should use keyup
instead, which is fired after the character has been added.
$('#search-vulc').on('keyup', function() {
var textinsert = ($(this).val()).toLowerCase();
alert(textinsert);
});
Use keyup
instead:
$('#search-vulc').on('keyup', function() {
var textinsert = ($(this).val()).toLowerCase();
alert(textinsert);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" size="150" style="width: 150px;" name="qu" id="search-vulc">
Updated fiddle: https://jsfiddle/06xg4c78/2/
if you use keydown
functions it works before dom gets the input value
Use keyup
which fires the event when you leave the key so it will get the value entered
$('#search-vulc').on('keyup', function() {
var textinsert = ($(this).val()).toLowerCase();
alert(textinsert);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" size="150" style="width: 150px;" name="qu" id="search-vulc">
本文标签: javascriptUsing keydown() why after first character if alert the value is emptyStack Overflow
版权声明:本文标题:javascript - Using keydown() why after first character if alert the value is empty? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745410650a2657453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论