javascript - 为日期时间选择器启用手动编辑
问题描述
我正在使用可以在此页面上找到的任何日期时间选择器插件 https://www.ama3.com/anytime/
我需要启用手动编辑字段的功能。该插件的作者为此提供了解决方案。不幸的是,这并不是我想要的。尽管一旦我单击日历图标并设置日期,该字段默认情况下是可编辑的,但我无法再手动编辑该字段。以下是我尝试过的插件官方页面的代码。有没有办法以某种方式修改它以使其按我想要的方式工作。谢谢你。
$('#ButtonCreationDemoButton').click(function(e) {
$('#ButtonCreationDemoInput').AnyTime_noPicker().AnyTime_picker().focus();
e.preventDefault();
});
<link href="https://www.ama3.com/anytime/anytime.5.2.0.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.ama3.com/anytime/anytime.5.2.0.js"></script>
<input type="text" id="ButtonCreationDemoInput"/>
<button id="ButtonCreationDemoButton">
<img src="calendar.png" alt="[calendar icon]"/>
</button>
解决方案
您需要解除 keydown 事件与输入字段的绑定,然后移除 readonly 属性以使其可编辑。
$('#ButtonCreationDemoButton').click(function(e) {
$('#ButtonCreationDemoInput').AnyTime_noPicker().AnyTime_picker().focus();
//remove readonly for the input field each button datepicker clicked
$('#ButtonCreationDemoInput').attr("readonly", false);
//unbind "keydown" event for the input field to make it editable
$('#ButtonCreationDemoInput').unbind("keydown");
e.preventDefault();
});
<link href="https://www.ama3.com/anytime/anytime.5.2.0.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.ama3.com/anytime/anytime.5.2.0.js"></script>
<input type="text" id="ButtonCreationDemoInput"/>
<button id="ButtonCreationDemoButton">
<img src="calendar.png" alt="[calendar icon]"/>
</button>
推荐阅读
- php - 在 For 循环中按日期对 XML 进行排序
- json - 修改 API JSON 返回的结尾
- jquery - 如何与jquery中的最后一个数据进行比较?
- c# - Xamrin Forms 的 Datanotations 问题
- domain-driven-design - 如何将验证规则放入存储库
- javascript - Jquery - 仅当 ajax 重新加载时值更改时才为 DIV 设置动画
- python - Tkinter (python 2.7) | 确定它的 tk 实例还是顶级实例
- numpy - 将numpy数组转换为沿不同深度维度堆叠的简单方法
- python-3.x - 如何在 Python 中授予对 AWS Lambda 的访问权限以读取外部网页?
- python - 如何在 Keras 损失函数中使用预训练的 TensorFlow 网络