javascript 将光标聚焦到隐藏字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18010412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Focus cursor to a hidden field
提问by poojitha
I have this script where i want cursor to be focused on hidden field but it doesn't seem to be working . When i make the field visible it seems to work.
我有这个脚本,我希望光标集中在隐藏字段上,但它似乎不起作用。当我使该字段可见时,它似乎起作用了。
$(document).ready(function(){
$("#card_number").focus();
$("#card_number").keypress(function() {
alert($("#card_number").val());
});
});
回答by sunn0
You can't focus on a hidden element.
你不能专注于隐藏的元素。
You can use opacity: 0
instead of display: none
and you will be able to focus on the element.
您可以使用opacity: 0
代替,display: none
您将能够专注于元素。
回答by poojitha
i found the solutions by trying out your comments , you can't use visibility: hidden; . You have to use opacity zero
我通过尝试您的评论找到了解决方案,您不能使用可见性:隐藏;. 你必须使用不透明度为零
#card_number{
opacity: 0;
}
Thanks fellas you were great This is for an auto login feature
谢谢伙计们,你们很棒 这是一个自动登录功能
回答by Ayman Farhat
As far as I know this isn't possible for a hidden field. What you might want to do is set the opacity of the input field to 0 via CSS.
据我所知,这对于隐藏字段是不可能的。您可能想要做的是通过 CSS 将输入字段的不透明度设置为 0。
回答by Leonel Sarmiento
The reason is the field is hidden.
原因是该字段被隐藏。
Don't hide the field just try to make the opacity to 0% using CSS
不要隐藏该字段,只需尝试使用 CSS 将不透明度设为 0%
回答by HaLeiVi
I wanted to do this same thing to use with a scanning device without the user seeing the numbers flying in.
我想用扫描设备做同样的事情,而用户不会看到飞进来的数字。
I tried Opcacity:0
but when it has focus you see the text cursor blinking which is not optimal.
我试过了,Opcacity:0
但是当它有焦点时,你会看到文本光标闪烁,这不是最佳的。
What I did, that works neatly is: {position: fixed; left: 300em}
我所做的,工作得很好: {position: fixed; left: 300em}
回答by Niths
Focus function is not work with hidden fields. Make the opacity to zero
焦点功能不适用于隐藏字段。使不透明度为零
回答by Abhishek Jain
You cant focus on any hidden field. What are you trying to achieve here.
你不能专注于任何隐藏的领域。你想在这里实现什么。