[英]How to write a script to restrict Ninja Form date picker for past dates?
I have a date picker field in my WordPress Website using the Ninja Forms Plugin.我使用 Ninja Forms 插件在我的 WordPress 网站中有一个日期选择器字段。 I don't think the plugin gives me the option to restrict past dates.
我不认为该插件让我可以选择限制过去的日期。 So I want to achieve that the user can not pick any past dates.
所以我想实现用户不能选择任何过去的日期。
I have of course tried to google a solution but I am not really good with javascript/jquery and don't understand how I can achieve this.我当然试图用谷歌搜索一个解决方案,但我对 javascript/jquery 并不是很擅长,也不明白我如何实现这一点。 Can anyone help?
任何人都可以帮忙吗? Many thanks in advance!
提前谢谢了!
It appears Ninja Forms has an advance date picker option I am not familiar with it but here is the link看起来 Ninja Forms 有一个提前日期选择器选项我不熟悉但这里是链接
NOW I don't know the exact Ninja Forms syntax but this should jolt your brain to get started.现在我不知道确切的 Ninja Forms 语法,但这应该会让你的大脑开始震动。 Other wise you can create a small custom
WP-plugin
using a add_filter()
and function
like add_filter()
您可以使用add_filter()
和类似的function
创建一个小的自定义WP-plugin
<?php
add_filter( 'ninja_form_date_limt' );
function ninja_form_date_limt() {
wp_enqueue_script(
'ninja_form_date_limt',
plugin_dir_url( __FILE__ ) . 'script.js',
array( 'jquery' ),
false,
true
);
}
This calls on a JQuery
or JavaScript
function in your theme/js/script.js
file or wherever you keep custom JS Which could will could use something like this.这会调用
theme/js/script.js
文件中的JQuery
或JavaScript
函数,或者您保留自定义 JS 的任何地方,这可以使用这样的东西。
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
dateObject.pikaday.setMinDate(tomorrow);
Found a solution to this by combining a few things I found across the web while trying to solve this problem.通过结合我在网络上找到的一些东西来解决这个问题,同时试图解决这个问题。
Simply add this code to any page where you have a date picker where you want to restrict past dates.只需将此代码添加到您想要限制过去日期的日期选择器的任何页面。 This one also restricts the current day so they have to pick starting tomorrow.
这也限制了当天,所以他们必须从明天开始选择。
<script>
jQuery(document).ready( function( $ ) {
var customDatePicker = Marionette.Object.extend( {
initialize: function() {
this.listenTo( Backbone.Radio.channel( 'flatpickr' ), 'init', this.modifyDatepicker );
},
modifyDatepicker: function( dateObject, fieldModel ) {
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
dateObject.set("minDate", tomorrow );
}
});
new customDatePicker();
} );
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.