2013-02-13 141 views
14

我已经在ASP.net中编写了一个网页登录表单。Android - html输入在软键盘打开时失去焦点(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()这没有帮助。


临时修复

现在我已经定居哈克修复,点击后700毫秒迫使焦点放回<input>元素:

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

我发现提到的[类似的问题(http://stackoverflow.com/questions/9147400/edittext-steals-focus),但这些似乎都与Java相关的 - 本地应用程序中。我无法在网页中找到任何其他关于此问题的提及。 – 2013-02-13 13:19:03

回答

31

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

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

我跑类似于下面的内容:

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

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

+1

答案是救生员。注意:根据我的经验,在Android 4.0.4上,此问题会影响本地浏览器,但不会影响其他浏览器(Chrome,Firefox或其他浏览器)。 – 2014-02-25 20:50:01

+0

万分感谢! – 2015-05-27 11:42:03

2

我必须找到其他的解决办法

首先,你必须创建一个功能

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

} 

,当你在输入点击调用该函数

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

这对我的作品

7

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

$(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 
    } 
}); 
+0

如果您有新问题,请点击[提问问题](http://stackoverflow.com/questions/ask)按钮。如果有助于提供上下文,请包含此问题的链接。 - [来自评论](/ review/low-quality-posts/10812017) – 2016-01-08 00:35:31

+0

@fish_ball,看起来好像这个问题措辞不佳,应该是'我有一个类似的问题',这不是一个新问题,这个是一个有效的答案。 – 2016-01-08 02:20:12

+0

工程就像一个魅力,谢谢! – 2016-08-25 13:39:40

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); 
}); 
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>