HTML 5 Input type='date' 禁用键盘输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29715655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:51:48  来源:igfitidea点击:

HTML 5 Input type='date' disable keyboard input

htmlgoogle-chrome

提问by b-m-f

I am working on a Chrome Packaged App so my code should only work in Chrome.

我正在开发 Chrome Packaged App,所以我的代码只能在 Chrome 中工作。

I have the following input

我有以下输入

<input type="date" />

https://jsfiddle.net/jhbo4q2k/

https://jsfiddle.net/jhbo4q2k/

On Chrome this automatically adds a DatePicker. I would like to only keep this Datepicker and disable the input by keyboard.

在 Chrome 上,这会自动添加一个 DatePicker。我只想保留这个 Datepicker 并禁用键盘输入。

Is this possible?

这可能吗?

EDIT:

编辑:

The accepted answer works. Just be wary of this

接受的答案有效。小心这个

https://developer.chrome.com/extensions/tut_migration_to_manifest_v2#inline_scripts

https://developer.chrome.com/extensions/tut_migration_to_manifest_v2#inline_scripts

You cant use inline scripts in a packaged app.

您不能在打包的应用程序中使用内联脚本。

回答by mohamedrias

You can use onkeydownand prevent user from entering the value.

您可以使用onkeydown并阻止用户输入该值。

<input type="date" onkeydown="return false" />

回答by Fran?ois Hereng

For ReactJS above solutions don't work

对于 ReactJS 以上解决方案不起作用

I had to do:

我必须做:

<input type="date" onKeyDown={(e) => e.preventDefault()} .... />

回答by Krishnamoorthy Acharya

Hi you can prevent keyboard popup by using onfocus="blur()". Since when the element has the focus we will remove the focus out of it(native keyboards won't show) however with onclick we can continue with our operations.

嗨,您可以使用 onfocus="blur()" 来防止键盘弹出。因为当元素获得焦点时,我们将移除它的焦点(本地键盘不会显示)但是通过 onclick 我们可以继续我们的操作。

 <input type="date" class="form-control"  onfocus="blur()" onclick="dosomework()" name="some-name" id="some-id" >
<script>

function dosomework(){
 alert('hi');
 }

<script>