jQuery 日期选择器,其中文本输入是只读的
我想使用 Jquery 日期选择器。我已经使用 alt 字段选项进行了设置。我在文本字段中显示 D/M/Y,但提交 YMD。到目前为止,一切工作正常,发送正确的数据等。
但是我想阻止用户手动输入日期。我最初将 INPUT 字段设置为禁用,该字段适用于除 IE 之外的所有浏览器。在 IE 中,它会弹出日期选择器,但单击日期后不会关闭。
有谁知道最好的方法来做到这一点?
I want to use the Jquery datepicker. I've got it setup using an the alt field option. I'm displaying D/M/Y in the text field, but submitting Y-M-D. Everything works fine so far, sending the correct data, etc.
However I want to stop the user from being able to manually type a date. I had originally set the INPUT field to disabled, which worked in every browser except IE. In IE it would popup the date picker but then not close after clicking a date.
Does anyone know the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了防止用户手动编辑输入,我将附加一个按键事件并返回 false/阻止其处理程序的默认值。
这样,如果他有 javascript,他可以使用日期选择器,如果没有,他可以手动编辑输入。
To prevent the user from manually editing the input, I would attach a keypress event and return false / prevent default from it's handler.
This way if he has javascript he can use the datepicker, and if not he can manually edit the input.
扩展 nc3b 的答案:
将阻止诸如退格键、删除键等键。否则,您可能有一个像 11/11/2012 这样的日期,并且用户可以将其退格到 11/11/201,从技术上讲,这仍然是一个有效的日期给JS。
Extending nc3b's answer:
will prevent keys such as backspace, delete, etc. Otherwise, you could have a date like 11/11/2012, and a user could backspace it to 11/11/201 and technically, thats still a valid date according to JS.
您还可以执行以下操作:
请参阅上面的
readonly
属性。如果您还希望日历在
获得焦点时显示,请添加以下内容:
You can also do this:
See the
readonly
attribute above.If you also want the calendar to show when the
<input>
has focus, add this:禁用输入字段后,使用 destroy 方法删除日期选择器。重新启用该字段时,只需再次重新创建日期选择器即可。
After disabling the input field, use the destroy method to remove the datepicker. When re-enabling the field, just re-create the datepicker again.