试图将焦点放在隐藏的文本框上 [英] trying to set focus on a hidden text box

查看:95
本文介绍了试图将焦点放在隐藏的文本框上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将重点放在隐藏的文本框中.我希望当包含文本框的正文或div加载时,焦点应放在特定的文本框上,以便键盘或任何其他设备的任何输入都被此元素捕获.我尝试了以下代码,但没有任何效果:

I am trying to set focus on a hidden text box. I want that when the body or div containing the text box loads the focus should be on the particular text box so that any input from key board or any other device is caught by this element. I have tried the following code with no effect:

<body>
    <input type="text" id="exp" maxlength="16"></input>
    <input type="text" id="exp2" maxlength="16"></input>
    <script>
        $("#exp").hide();
        $("#exp").focus();
        $("#exp2").keypress(function(){
            alert($("#exp").val());
        });
    </script>
</body>

提出任何建议.最好使用jquery解决方案.

make any suggestions. jquery solution will be preferred.

推荐答案

您不能将焦点设置为通过hide方法隐藏的文本框.相反,您需要将其移出屏幕.

You can't set focus to a text box that is hidden through the hide method. Instead, you need to move it off screen.

<body>
<!-- it's better to close inputs this way for the sake of older browsers -->
<input type="text" id="exp" maxlength="16" />
<input type="text" id="exp2" maxlength="16" />
<script>
// Move the text box off screen
$("#exp").css({
    position: 'absolute',
    top: '-100px'
});
$("#exp").focus();
$("#exp2").keypress(function(){
alert($("#exp").val());
});
</script>
</body>

这篇关于试图将焦点放在隐藏的文本框上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆