2013-02-13 125 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> 
相關問題