简体   繁体   English

如何防止键盘在移动设备上弹出?

[英]How to prevent the keyboard from popping up on mobile devices?

http://api.jqueryui.com/spinner/ http://api.jqueryui.com/spinner/

I am trying to use the jQuery spinner above in my website (a demo of it is available at the bottom of API). 我正在尝试在我的网站中使用上面的jQuery微调器(在API底部提供其演示)。

It works really work on computers, but on mobile devices, the keyboard very annoyingly pops up every time one clicks the up/down buttons. 它确实可以在计算机上工作,但是在移动设备上,每当单击向上/向下按钮时,键盘都会非常烦人地弹出。 Is it possible to prevent this from happening? 有可能防止这种情况发生吗? The spinner does not really respond well to native functions like .on('click'), instead it has its own functions. 微调器对诸如.on('click')之类的本机功能的响应不是很好,而是具有自己的功能。

How do I modify the code so that the keybooard only shows up when the textbox is clicked, not the up-down buttons? 如何修改代码,以便仅在单击文本框时才显示键盘,而不是在单击按钮时才显示?

This was my attempt, it does not work: 这是我的尝试,它不起作用:

$( function() {
    $('.ui-spinner a').on('click', function() {
        $(':focus').blur();
});

})  // Updated code, I can now see the focus being lost on desktops, but still not mobile devices

Note: I got the class name by inspecting the code generated when the spinner is created.Also, I am super new to web development so I am not sure whether I am missing an easy approach. 注意:我通过检查创建微调器时生成的代码来获得类名。此外,我是Web开发的超级新手,因此不确定是否缺少一种简单的方法。

When step up button is clicked, the input will be focus, so mobile device display keyboard, the solution is add readonly attribute, when user click input box, remove it, on blur, add readonly attribute again. 单击升压按钮时,输入将成为焦点,因此移动设备显示键盘,解决方法是添加只读属性,当用户单击输入框时,将其删除,在模糊时,再次添加只读属性。

see the code snippet to understand 查看代码片段以了解

 $( "#spinner" ).spinner(); $( "#spinner" ).click(function(event){ $(this).removeAttr('readonly').select(); }); $( "#spinner" ).blur(function(event){ $(this).attr('readonly', 'readonly'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <input id="spinner" > 

您只需要将readonly属性添加到input标签。

<input id="spinner" readonly>

 $(document).ready(function() { $( ".spinner" ).spinner(); $( ".spinner" ).click(function(event){ $(this).removeAttr('readonly').select(); }); $( ".spinner" ).blur(function(event){ $(this).attr('readonly', 'readonly'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <input class="spinner" min="1" max="9" value="1" height="40px" readonly="readonly" onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57"> 

请设置onfocus =“ blur()”

<input id="spinner" onfocus="blur()"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM