javascript 在输入文本字段中禁用键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29007801/
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-10-28 09:57:01  来源:igfitidea点击:

Disable Keyboard in Input Textfield

javascriptjqueryhtml

提问by n00dle

I have a jquery Datetimepicker and i want to disable the keyboard input. I want only edit this field over the picker (Modalbox).

我有一个 jquery Datetimepicker,我想禁用键盘输入。我只想通过选择器(Modalbox)编辑此字段。

回答by hamed

You should disable keyboard on your input like this:

您应该像这样在输入中禁用键盘:

$(function() {
   $('#datepicker').keypress(function(event) {
       event.preventDefault();
       return false;
   });
});

And Here is a working fiddle.

这里是工作提琴

回答by rmabs

readonlyattribute on the text input should work for you, use this property in input field either by manually entering or you can set this attribute using Javascript or jQuery.

readonly文本输入上的属性应该适合您,通过手动输入在输入字段中使用此属性,或者您可以使用 Javascript 或 jQuery 设置此属性。

Set a text field to read-only using Script:

使用脚本将文本字段设置为只读:

document.getElementById("myText").readOnly = true;

Or in HTML:

或者在 HTML 中:

<input type="text" name="textName" placeholder="Select date" readonly>

回答by I'm Geeker

Try this prop()

试试这个道具()

// Disable #x
$( "#x" ).prop( "disabled", true );

// Enable #x
$( "#x" ).prop( "disabled", false );