jQuery 使用输入掩码时在掩码中指定日期格式?

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

Specify date format in mask when using an input mask?

jquerydate-formatmaskedinput

提问by Adam

I've been using the jquery masked input pluginto apply a date mask to text inputs. This works pretty well, but I want the displayed mask to also show the format. That is, instead of the mask being

我一直在使用 jquery屏蔽输入插件将日期掩码应用于文本输入。这工作得很好,但我希望显示的掩码也显示格式。也就是说,而不是掩码

__/__/____

I want to be able to specify the format by using a mask of, for example,

我希望能够通过使用掩码来指定格式,例如,

dd/mm/yyyy

or (which looks the same with the current mask but is fundamentally different)

或(与当前掩码看起来相同但根本不同)

mm/dd/yyyy

I can't see how to do this with that plugin (it only accepts one character as the mask character as far as I can see). Is it possible with the current plugin? Does anyone know of an alternative plugin (ideally using jQuery) which can do this? I haven't been able to find anything.

我看不出如何使用该插件执行此操作(就我所见,它只接受一个字符作为掩码字符)。当前的插件可以吗?有谁知道可以做到这一点的替代插件(最好使用 jQuery)?我一直无法找到任何东西。

回答by Whisk

This is now supported by http://digitalbush.com/projects/masked-input-plugin/- you just have to specify the full placeholder (see the date demo on that site):

这现在由http://digitalbush.com/projects/masked-input-plugin/支持- 您只需要指定完整的占位符(请参阅该站点上的日期演示):

$('#date').mask('99/99/9999',{placeholder:"mm/dd/yyyy"});

$('#date').mask('99/99/9999',{placeholder:"mm/dd/yyyy"});

回答by Kevin Lee Garner

I had a similar requirement recently, and the only thing I could find that didn't require me to do a lot of work on tweaking the plugin was to use this modified date masking plugin. It seems to be doing the job so far.

我最近有一个类似的需求,我能找到的唯一不需要我在调整插件上做很多工作的东西就是使用这个修改后的日期屏蔽插件。到目前为止,它似乎正在完成这项工作。

If you want to test it out, head to this page.

如果您想对其进行测试,请前往此页面

The basic implementation we've used with this modified date masker is as follows:

我们与这个修改后的日期掩码器一起使用的基本实现如下:

$('#yourInputBoxHere').mask("99/99/9999", {placeholder: 'MM/DD/YYYY' });

I hope this helps.

我希望这有帮助。

Update: A word of warning: this plugin seems to be incompatible with later versions of jQuery. I think it's safe for jQuery 1.4 and lower.

更新:警告:这个插件似乎与更高版本的 jQuery 不兼容。我认为它对于 jQuery 1.4 及更低版本是安全的。

回答by Martin

You can set the value of the input as a default value like this:

您可以将输入的值设置为默认值,如下所示:

$('#someId').mask("99/99/9999").val("dd/mm/aaaa");

回答by ShankarSangoli

You can specify your own mask to this plugin using the $.mask.definitionsand then use it in your code.

您可以使用 为该插件指定您自己的掩码$.mask.definitions,然后在您的代码中使用它。