20

我在 ASP.net 中编写了一个网页登录表单。

使用 Nexus 4、库存 Android 4.2.1、原生 Chrome 浏览器 - 当我单击<input>字段时,出现软键盘,然后字段立即失去焦点。我必须<input>在键盘已经打开的情况下再次单击该字段才能实际输入文本。

这不会在桌面版 Chrome 中发生。

我在 ASP 用户控件中有以下登录表单:

<asp:Login ID="login_form" runat="server" OnLoginError="OnFail" OnLoggedIn="OnLoggedIn" RenderOuterTable="false" RememberMeSet="True">
    <LayoutTemplate>

        <asp:Panel runat="server" DefaultButton="LoginButton" CssClass="members-login">
            <fieldset>
                <label for="UserName">Username</label>
                <asp:TextBox ID="username" placeholder="e.g. joebloggs12" runat="server"></asp:TextBox>

                <label for="Password">Password</label>
                <asp:TextBox ID="password" runat="server" placeholder="e.g. 1234" TextMode="Password"></asp:TextBox>

                <label id="RememberMe">Remember me</label>
                <asp:CheckBox ID="RememberMe" runat="server" />

                <asp:Button CssClass="button" ID="LoginButton" runat="server" CommandName="Login" Text="SUBMIT" />
            </fieldset>
        </asp:Panel>         
    </LayoutTemplate>
</asp:Login>

在浏览器中看起来像这样:

<div class="members-login" onkeypress="javascript:return WebForm_FireDefaultButton(event, &#39;ContentPlaceHolderDefault_contentBody_masterContent_login_form_LoginButton&#39;)">

    <fieldset>
        <label for="UserName">Username</label>
        <input name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$contentBody$masterContent$login_form$username" type="text" id="ContentPlaceHolderDefault_contentBody_masterContent_login_form_username" placeholder="e.g. joebloggs12" />

        <label for="Password">Password</label>
        <input name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$contentBody$masterContent$login_form$password" type="password" id="ContentPlaceHolderDefault_contentBody_masterContent_login_form_password" placeholder="e.g. 1234" />

        <label id="RememberMe">Remember me</label>
        <input id="ContentPlaceHolderDefault_contentBody_masterContent_login_form_RememberMe" type="checkbox" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$contentBody$masterContent$login_form$RememberMe" checked="checked" />

        <input type="submit" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$contentBody$masterContent$login_form$LoginButton" value="SUBMIT" id="ContentPlaceHolderDefault_contentBody_masterContent_login_form_LoginButton" class="button" />
    </fieldset>
</div>  

我怀疑 ViewState 正在以某种方式窃取焦点。但我试过event.stopPropogation()了,但没有帮助。


临时修复

<input>现在我已经解决了一个hacky修复,在点击后700毫秒强制焦点回到元素上:

jQuery('input').bind('click',function(evt) {
    var focusClosure = (function(inputElem) {return function() {console.log(inputElem.focus());}}($(evt.target)));
    window.setTimeout(focusClosure, 700);
});
4

6 回答 6

43

我发现这与 ASP.net 或 ViewState 无关。

当 Android 键盘出现时,浏览器窗口会调整大小- 触发调整大小事件

我正在运行类似于以下内容的内容:

// Move the navigation on resize
$(window).bind('resize', function() {
    if(maxWidth <= 710px) {
        $('.nav').after($('.main-content'));
    }
});

这移动了 DOM 中的导航。我猜这会重绘 DOM,因此会导致 DOM 中的任何内容失去焦点。我在调整大小时阻止了这种情况发生 - 使它只在初始页面加载时发生。

于 2013-02-13T15:31:59.740 回答
9

我有一个类似的问题,如果输入是焦点,我防止调整大小执行的方法解决了这个问题:

$(window).bind('resize', function() {
    if ($("input").is(":focus")) {
        // input is in focus, don't do nothing or input loses focus and keyboard dissapears
    } else {
        // do whatever you should do if resize happens
    }
});
于 2015-11-25T20:41:39.260 回答
4

我找到了其他解决方案

首先你必须创建一个函数

function getFocus(campo){
    $(window).bind('resize', function() {
        if(windowWidth <= 1025) {
            $(campo).focus();
        }
    }); 

}   

当您单击输入时,您会调用该函数

$('input').click(function(){
    getFocus(this); 
});

这对我有用

于 2015-10-07T09:22:58.723 回答
0

需要 jQuery

我自己也遇到过这个问题,最终使用了这个解决方案。它可能对其他人有用。就我而言,调整大小使搜索栏失去焦点。

// Makes it so that click events are fired on a mobile device.
$('#searchbar').each(function(){
    this.onclick = function() {}
});

// Runs an interval 10 times with a 50ms delay. Forces focus.
$("#searchbar").click(function() {
    var i = 0;
    var interval = window.setInterval(function(){
        if(i > 10) {
            clearInterval(interval);
        }
        $("#searchbar").focus();
        i++;
    }, 50);
});
于 2017-10-01T12:03:47.823 回答
0

对于那些在 WordPress 中遇到此问题的人,请将以下代码插入标题中。

<script type="text/javascript">
 jQuery(function($) {
 $(window).bind('resize', function() {
 if ($("input").is(":focus")) {
 var focusClosure = (function(inputElem) {return function() 
 {console.log(inputElem.focus());}}($(evt.target)));
window.setTimeout(focusClosure, 700);
 } else {
    // Other resize functions
}
}); 
});
</script>
于 2018-02-02T10:07:49.280 回答
0

我有$(window).resize(..)我想保留的自定义代码,我想将此解决方法限制为仅受影响的特定用例(仅限 Android 移动设备,文本输入或 TextArea 焦点),所以我想出了这个:

function isAndroidTextInputFocus() {
    var userAgent = navigator.userAgent.toLowerCase();
    var isAndroid = userAgent.indexOf("android") != -1;

    var result = (isAndroid && 
             ($("input[type=text]").is(":focus") || $("textarea").is(":focus"))     
           );

    return result;
}

然后我像这样调整了我的 $(window).resize(..) :

$(window).resize(function(e) {
    if (!isAndroidTextInputFocus()) {       
        // ... Proceed with my custom Window Resize logic
    }
}); 
于 2018-04-05T19:59:58.310 回答