php 如何禁用 JQuery UI Datepicker 字段的手动输入?

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

How to disable manual input for JQuery UI Datepicker field?

phpjquery-uidatepicker

提问by BeeDog

I decided to use the JQuery UI Datepicker script for picking dates. Below is part of my code, and the way I integrated it into my PHP page:

我决定使用 JQuery UI Datepicker 脚本来选择日期。下面是我的代码的一部分,以及我将它集成到我的 PHP 页面的方式:

<link type="text/css" href="css/south-street/jquery-ui-1.8.6.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.6.custom.min.js"></script>
<script type="text/javascript">
$(function(){
    // Datepicker
    $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
        function() { $(this).addClass('ui-state-hover'); }, 
        function() { $(this).removeClass('ui-state-hover'); }
    );
});
</script>

And:

和:

    // date picker (embeds JQuery script)
echo '<label for="datepicker">Date: </label>';
echo '<input type="text" name="datepicker" id="datepicker" />';
echo '<div id="datepicker"></div>';

Pretty much according to the JQuery UI instructions.

几乎根据 JQuery UI 说明。

Now my question is: how can I disable manual inputs in the text input field? I only want the JQuery Datepicker script to be able to fill the text field. As far as I can see, the script currently prevents the user from entering any non-numerical characters into the text field, but any combination of numbers is allowed (therefore, gibberish like "95460829468" is allowed).

现在我的问题是:如何在文本输入字段中禁用手动输入?我只希望 JQuery Datepicker 脚本能够填充文本字段。据我所知,该脚本目前阻止用户在文本字段中输入任何非数字字符,但允许任何数字组合(因此,允许使用像“95460829468”这样的乱码)。

回答by Matthew Jacobs

When you make the input, set it to be readonly.

进行输入时,将其设置为只读。

<input type="text" name="datepicker" id="datepicker" readonly="readonly" />

回答by Karim Samir

I think you should add style="background:white;"to make looks like it is writable

我认为你应该添加 style="background:white;" 让它看起来像是可写的

<input type="text" size="23" name="dateMonthly" id="dateMonthly" readonly="readonly"   style="background:white;"/>