禁用输入文本字段中的键盘

Disable Keyboard in Input Textfield

本文关键字:键盘 字段 文本 输入      更新时间:2023-09-26

我有一个jquery Datetimepicker,我想禁用键盘输入。我只想在选择器(模态框)上编辑此字段。

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

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

这是一个工作小提琴。

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

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

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

或在 HTML 中:

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

试试这个 prop()

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