altFormat 在 jQuery 日期选择器输入字段中不起作用

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

altFormat not working in jQuery datepicker input field

jqueryjquery-uidatepickerjquery-ui-datepicker

提问by Brad

I have date field (id begin-date)

我有日期字段(ID 开始日期)

$( "#begin-date" ).datepicker({ 
  minDate: -20,
  maxDate: "+1M +10D",
  dateFormat: "yy-mm-dd",
  altFormat: "yymmdd"
});

On post, it prints the format as yy-mm-dd (2010-12-08), when it should print as yymmdd (20101208)

在发布时,它将格式打印为 yy-mm-dd (2010-12-08),而应该打印为 yymmdd (20101208)

Any ideas of why it is not posting it properly with altFormat set?

为什么它没有使用 altFormat 设置正确发布它的任何想法?

input field rendered:

输入字段呈现:

<input type="text" name="begin_date" id="begin-date" class="validate[required]" value="" />

回答by Nick Craver

The altFormatoption doesn't control the formatting of the input with the date picker, but the format of an alternate (usually hidden) field specified by the altFieldoption, like this:

altFormat选项不控制与该日期选择器输入的格式,但可替换(通常是隐藏的)字段中指定通过的格式altField选项,如:

$("#begin-date").datepicker({ 
  minDate: -20,
  maxDate: "+1M +10D",
  dateFormat: "yy-mm-dd",
  altFormat: "yymmdd",
  altField: "#alt-date"
});

You can test it out here; what you probably want is to just put the nameon that alt field and that's what'll get posted...without a namethe field with the date picker won't get serialized/submitted, for example:

你可以在这里测试一下;您可能想要的是将 放在该namealt 字段上,这就是将要发布的内容……如果没有name带有日期选择器的字段,将不会被序列化/提交,例如:

<input type="text" id="begin-date" class="validate[required]" />
<input type="text" id="alt-date" name="begin_date" />

回答by user2231487

the altFormat must work with altField;

altFormat 必须与 altField 一起使用;

<input type="text" name="pushTime" class="datetime" id="pushTime"/> $(".datetime").datepicker({altFormat:"yy-mm-dd",altField: "#pushTime"});

<input type="text" name="pushTime" class="datetime" id="pushTime"/> $(".datetime").datepicker({altFormat:"yy-mm-dd",altField: "#pushTime"});