简体   繁体   English

提交表单时将 Datepicker 转换为 MM/DD/YYYY 格式

[英]Convert Datepicker to MM/DD/YYYY Format when submitting form

I have created a subscription order form on my website with Gravity Forms.我在我的网站上使用 Gravity Forms 创建了一个订阅订单。 The form includes a date field formatted with a Datepicker.该表单包括一个使用 Datepicker 格式化的日期字段。 I added custom Javascript to limit the viable selection of dates to anything a day away.我添加了自定义 Javascript 以将可行的日期选择限制为一天后的任何时间。 That code is as follows:该代码如下:

$('#input_14_11').datepicker({
minDate : '+1d'});

It serves its purpose and users are not able to select any past dates from the datepicker but a new issue has risen: The form cannot submit but instead returns an error message of "Please enter a valid date in the format (mm/dd/yyyy)."它达到了它的目的,用户无法 select 日期选择器的任何过去日期,但出现了一个新问题:表单无法提交,而是返回错误消息“请以格式(mm/dd/yyyy)输入有效日期)。” When I select a date it displays in the field the full date, example: (July 27,2022).当我 select 一个日期时,它会在字段中显示完整日期,例如:(2022 年 7 月 27 日)。 However, when I manually enter a date in the specified format (07/27/2022), the form submits without an issue.但是,当我以指定格式(07/27/2022)手动输入日期时,表单提交没有问题。

I essentially need to convert that selected "July 27,2022" into the correct format of 07/27/2022 when a user hits submit on the form.当用户在表单上点击提交时,我基本上需要将选择的“2022 年 7 月 27 日”转换为 07/27/2022 的正确格式。 Example of returned error返回错误示例

$( "#input_14_11" ).datepicker({ dateFormat: 'mm-dd-yy',minDate : '+1d' });

Instead of instantiating a new datepicker you could customize the one instantiated by Gravity Forms by using the gform_datepicker_options_pre_init filter eg您可以使用gform_datepicker_options_pre_init过滤器自定义由 Gravity Forms 实例化的日期选择器,而不是实例化一个新的日期选择器,例如

gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    if ( formId == 14 && fieldId == 11 ) {
        optionsObj.minDate = '+1d';
    }
    return optionsObj;
});

By using this approach all the other datepicker properties, such as the date format, will remain the same.通过使用这种方法,所有其他日期选择器属性(例如日期格式)将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM