使用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
发布评论