Is there a numeric keypad for jQuery Mobile?

爱⌒轻易说出口 提交于 2019-12-24 14:47:23

问题


I have a peripheral for the iPad that needs to set the focus on a numeric field to allow the user to enter a qty.

My problem is: When my JavaScript sets the focus on the field, the keyboard doesn't slide up.

One possible solution would be to just throw 10 buttons up on the screen, but before I do that, I thought I would ask the community if a nicely styled keypad has already been done in jQuery mobile.


回答1:


Try setting the input type to:

  • type="email"
  • type="url"
  • type="tel"

Or (Ref: Text, Web, and Editing Programming Guide for iOS)

<input type="text" pattern="\d*"></input>



回答2:


Updated: JSFiddle with an example. (I am not the author)

Here is a decent custom jQuery keypad to check out: Simple jQuery Keypad

HTML:

<input style="background: white; color: black;" type="text" readonly="readonly" id="myInput"/>
<table class="ui-bar-a" id="n_keypad" style="display: none; -khtml-user-select: none;">
<tr>
   <td><a data-role="button" data-theme="b" class="numero">7</a></td>
   <td><a data-role="button" data-theme="b" class="numero">8</a></td>
   <td><a data-role="button" data-theme="b" class="numero">9</a></td>
   <td><a data-role="button" data-theme="e" class="del">Del</a></td>
</tr>
<tr>
   <td><a data-role="button" data-theme="b" class="numero">4</a></td>
   <td><a data-role="button" data-theme="b" class="numero">5</a></td>
   <td><a data-role="button" data-theme="b" class="numero">6</a></td>
   <td><a data-role="button" data-theme="e" class="clear">Clear</a></td>
</tr>
<tr>
   <td><a data-role="button" data-theme="b" class="numero">1</a></td>
   <td><a data-role="button" data-theme="b" class="numero">2</a></td>
   <td><a data-role="button" data-theme="b" class="numero">3</a></td>
   <td><a data-role="button" data-theme="e">&nbsp;</a></td>
</tr>
<tr>
   <td><a data-role="button" data-theme="e" class="neg">-</a></td>
   <td><a data-role="button" data-theme="b" class="zero">0</a></td>
   <td><a data-role="button" data-theme="e" class="pos">+</a></td>
   <td><a data-role="button" data-theme="e" class="done">Done</a></td>
</tr>
</table>

JS:

$(document).ready(function(){
  $('#myInput').click(function(){
      $('#n_keypad').fadeToggle('fast');
  });
  $('.done').click(function(){
      $('#n_keypad').hide('fast');
  });
  $('.numero').click(function(){
    if (!isNaN($('#myInput').val())) {
       if (parseInt($('#myInput').val()) == 0) {
         $('#myInput').val($(this).text());
       } else {
         $('#myInput').val($('#myInput').val() + $(this).text());
       }
    }
  });
  $('.neg').click(function(){
      if (!isNaN($('#myInput').val()) && $('#myInput').val().length > 0) {
        if (parseInt($('#myInput').val()) > 0) {
          $('#myInput').val(parseInt($('#myInput').val()) - 1);
        }
      }
  });
  $('.pos').click(function(){
      if (!isNaN($('#myInput').val()) && $('#myInput').val().length > 0) {
        $('#myInput').val(parseInt($('#myInput').val()) + 1);
      }
  });
  $('.del').click(function(){
      $('#myInput').val($('#myInput').val().substring(0,$('#myInput').val().length - 1));
  });
  $('.clear').click(function(){
      $('#myInput').val('');
  });
  $('.zero').click(function(){
    if (!isNaN($('#myInput').val())) {
      if (parseInt($('#myInput').val()) != 0) {
        $('#myInput').val($('#myInput').val() + $(this).text());
      }
    }
  });
});


来源:https://stackoverflow.com/questions/8619459/is-there-a-numeric-keypad-for-jquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!