使用jQuery在HTML输入字段中检查输入实时/使用击键(Checking for input live/with key strokes in HTML input field with jQue

编程入门 行业动态 更新时间:2024-10-24 18:27:31
使用jQuery在HTML输入字段中检查输入实时/使用击键(Checking for input live/with key strokes in HTML input field with jQuery)

一旦字段有任何输入,我需要找到一种方法来隐藏ID'忘记'的范围。 “忘记”跨度应该可见的唯一时间是字段为空时。

<form> <input type="text" name="username" placeholder="Username or E-mail" autofocus><br> <input type="password" name="password" id='pass' placeholder="Password"><br> <span id="forgot">( <a class="forgot" href="<?php echo $dom; ?>forgot/">forgot?</a> )</span> <input type="submit" value="Login"> </form>

我目前有这个脚本....

$(document).ready(function() { $(document).keypress(function() { var value=$.trim($("#pass").val()); if (value.length==0) { $('#forgot').css("display","initial"); } else { $('#forgot').css("display","none"); } } });

有没有更好的方法来检查用户的实时输入并让它运行一个函数?

I need to find a way to hide the span with the ID 'forgot' as soon as the field has any input. The only time the 'forgot' span should be visible is when the field is empty.

<form> <input type="text" name="username" placeholder="Username or E-mail" autofocus><br> <input type="password" name="password" id='pass' placeholder="Password"><br> <span id="forgot">( <a class="forgot" href="<?php echo $dom; ?>forgot/">forgot?</a> )</span> <input type="submit" value="Login"> </form>

I currently have this script....

$(document).ready(function() { $(document).keypress(function() { var value=$.trim($("#pass").val()); if (value.length==0) { $('#forgot').css("display","initial"); } else { $('#forgot').css("display","none"); } } });

Is there any better way to check for live input from a user and get it to run a function?

最满意答案

$(document).ready(function() { $(document).on('keypress', '#pass', function() { var value = $(this).val(); if (value.length) { // $('#forgot').css("display", "block"); // Or $('#forgot').show(); } else { // $('#forgot').css("display", "none"); // Or $('#forgot').hide(); } }); }); 您不应该在document上添加事件,而是添加pass 使用show / hide功能 使用display : block而不是initial 无需与==0进行比较 $(document).ready(function() { $(document).on('keypress', '#pass', function() { var value = $(this).val(); if (value.length) { // $('#forgot').css("display", "block"); // Or $('#forgot').show(); } else { // $('#forgot').css("display", "none"); // Or $('#forgot').hide(); } }); }); You shouldn't be adding event on document, add on pass instead Use show/hide functions Use display: block instead of initial No need to compare to ==0

更多推荐

type,<input,value,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-08-06 20:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   实时   击键   jQuery   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!