简体   繁体   English

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

[英]HTML 5 Input type='date' disable keyboard input

I am working on a Chrome Packaged App so my code should only work in Chrome.我正在开发 Chrome 打包应用程序,因此我的代码只能在 Chrome 中运行。

I have the following input我有以下输入

<input type="date" />

https://jsfiddle.net/jhbo4q2k/ https://jsfiddle.net/jhbo4q2k/

On Chrome this automatically adds a DatePicker.在 Chrome 上,这会自动添加一个 DatePicker。 I would like to only keep this Datepicker and disable the input by keyboard.我只想保留此 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.您不能在打包的应用程序中使用内联脚本。

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

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

For ReactJS above solutions don't work对于 ReactJS 以上解决方案不起作用

I had to do:我必须做:

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

Hi you can prevent keyboard popup by using onfocus="blur()".嗨,您可以使用 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.因为当元素获得焦点时,我们将移除它的焦点(本地键盘不会显示)但是通过 onclick 我们可以继续我们的操作。

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

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

<script>

Easy Way To Enable & Disable Input Date:启用和禁用输入日期的简单方法:

Step 1: create input date.步骤 1:创建输入日期。

 <input type="date" id="dateEnd">

Step 2: create enable and disable button第 2 步:创建启用和禁用按钮

 <button onclick="disableBtn()">Disable Date Field</button> <button onclick="undisableBtn()">Undisable Date Field</button>

Step 3: Javascript for enabling and disabling第 3 步:用于启用和禁用的 Javascript

 <script> function disableBtn() { document.getElementById("dateEnd").disabled = true; } function undisableBtn() { document.getElementById("dateEnd").disabled = false; } </script>

Hope, this may help you.希望,这可以帮助你。

use this : 用这个 :

 <input type="date" id="date_input">

with jquery : 与jquery:

  $("#date_input").on("keydown", function(e) {
     e.preventDefault();
   });

If using django forms;如果使用 Django 表单;

datetime = forms.DateTimeField(widget=forms.DateTimeInput(attrs={
            'onkeydown': 'return false' # If you want to disable the keyboard
            "onfocus": 'blur()' # If you also want to disable virtual keyboard(on smartphones).
        }))

OR或者

document.getElementById('id_datetime').onkeydown = (e) => {
    e.preventDefault();
}
document.getElementById('id_datetime').setAttribute('onfocus', 'blur()');

Django simply adds 'id' in front of the input field name and sets that as its id. Django 只是在输入字段名称前添加“id”并将其设置为它的 id。 Here the input field name is datetime , so the id will be id_datetime .这里的输入字段名称是datetime ,因此 id 将是id_datetime

e.preventDefault() will do the job... e.preventDefault() 将完成这项工作......

 const inputDate = document.querySelector("input"); inputDate.addEventListener("keydown", function (e) { e.preventDefault(); });
 <input type="date">

Disabling keyboard input is a great way to make your application less accessible to people with disabilities and make it less user friendly in general, in my opinion keyboard input is much easier to use than date pickers, especially if the expected format is clear.禁用键盘输入是一种很好的方法,它可以让残障人士更难访问您的应用程序,并降低它的用户友好性,在我看来,键盘输入比日期选择器更容易使用,尤其是在预期格式明确的情况下。 You'd be better off doing some basic field validation to ensure that the input is sensible before letting it be submitted, the HTML date field already has strong validation on it by default on Chrome Firefox and Edge.你最好做一些基本的字段验证,以确保输入在提交之前是合理的,默认情况下,HTML 日期字段在 Chrome Firefox 和 Edge 上已经有强大的验证。

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

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