2011-05-04 108 views
9

我有一個jQuery的移動網站與一個由4個引腳輸入框組成的html表單。我希望用戶能夠在每個輸入字段中輸入一個別針,而不必按iphone鍵盤上的「下一步」按鈕。我嘗試了以下方法,儘管它似乎將焦點設置爲第二個輸入並插入值,但鍵盤消失,因此用戶仍然必須通過輕擊事件激活所需的輸入。jQuery Mobile焦點鍵盤上的下一個輸入

$('#txtPin1').keypress(function() { 
     $('#txtPin1').bind('change', function(){ 
      $("#txtPin1").val($("#txtPin1").val()); 
     }); 
     $("#txtPin2").focus(); 
     $("#txtPin2").val('pin2'); 
}); 

有,我應該分配給$( 「#txtPin2」)不同的事件?

我試圖實現http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/這也是,但我發現它適用於android而不適用於iphone。

任何幫助,非常感謝它。

+0

讓我明確了獎金:看來,iOS設備唐不接受焦點()和/或select()方法。該字段明顯被選中/激活,但似乎沒有「點擊」或「點按」事件來激活設備鍵盤。 – 2013-03-29 11:30:53

回答

3

活生生的例子:http://jsfiddle.net/Q3Ap8/22/

JS:

$('.txtPin').keypress(function() { 
    var value = $(this).val(); 

    // Let's say it's a four digit pin number 
    // Value starts at zero 
    if(value.length >= 3) { 
     var inputs = $(this).closest('form').find(':input'); 
     inputs.eq(inputs.index(this)+ 1).focus(); 
    } 
}); 

HTML:

<div data-role="page" data-theme="b" id="jqm-home"> 
    <div data-role="content"> 
     <form id="autoTab"> 
      <label for="name">Text Pin #1:</label> 
      <input type="text" name="txtPin1" id="txtPin1" value="" class="txtPin" placeholder="Please enter Pin #1"/> 

      <br /> 
      <label for="name">Text Pin #2:</label> 
      <input type="text" name="txtPin2" id="txtPin2" value="" class="txtPin" placeholder="Please enter Pin #2"/> 

      <br /> 
      <label for="name">Text Pin #3:</label> 
      <input type="text" name="txtPin3" id="txtPin3" value="" class="txtPin" placeholder="Please enter Pin #3"/> 

      <br /> 
      <label for="name">Text Pin #4:</label> 
      <input type="text" name="txtPin4" id="txtPin4" value="" class="txtPin" placeholder="Please enter Pin #4" maxlength="4"/> 
     </form> 
    </div> 
</div> 
+1

感謝您的幫助。我嘗試了你的代碼,雖然焦點被設置爲下一個輸入,但是iphones鍵盤被隱藏,所以它仍然需要用戶點擊聚焦的輸入框。該事件需要在關鍵幀上觸發,以便在移動到下一個之前將值寫入第一個輸入。有沒有辦法觸發下一個輸入的按鍵? – user738175 2011-05-05 16:16:58

+0

我遇到了與上述相同的問題! – 2013-03-29 09:21:46

3

我試圖在Android和它的作品。我無法在iPhone上查看它。 檢查:http://jsfiddle.net/Q3Ap8/276/ [直接鏈接:http://fiddle.jshell.net/Q3Ap8/276/show/]

$('.txtPin').keydown(function() { 
    that=this; 
    setTimeout(function(){ 
     var value = $(that).val(); 
     if(value.length > 3) { 
     $(that).next().next().next().focus().tap(); 
     } 
    },0); 
}); 

是Android版其他的解決方案是: http://jsfiddle.net/Q3Ap8/277/ [http://jsfiddle.net/Q3Ap8/277/show]

$('.txtPin').keydown(function() { 
    that=this; 
    setTimeout(function(){ 
     var value = $(that).val(); 
     if(value.length > 3) { 
     $(that).next().next().next().click(); 
     } 
    },0); 
}); 
$('.txtPin').click(function() { 
    $(this).focus().tap(); 
}); 
+0

我要測試這個:)此刻我的iPhone崩潰。我想我從Cydia安裝了一個不好的調整器,哈哈。 – 2013-03-29 10:38:18

+0

似乎沒有在iPhone上工作。嘗試了一些東西 – 2013-03-29 10:53:04

+0

我測試了另一個(見編輯)。 – Saram 2013-03-29 12:34:41

相關問題