Take the User to a Page When a Date is Selected in Flatpickr Calendar, Along with the Selected Date as Parameter

huangapple go评论64阅读模式
英文:

Take the User to a Page When a Date is Selected in Flatpickr Calendar, Along with the Selected Date as Parameter

问题

可以使用JavaScript来实现这个功能。当用户在flatpickr日历中选择一个日期时,你可以使用JavaScript来捕获所选日期,然后构建一个包含所选日期的URL,并将浏览器重定向到该URL。以下是一个示例代码片段,演示了如何实现这一功能:

// 获取flatpickr日历的DOM元素
var flatpickrCalendar = document.getElementById('yourFlatpickrElementId');

// 初始化flatpickr日历
flatpickr(flatpickrCalendar, {
    // 设置日期选择后的回调函数
    onChange: function(selectedDates, dateStr, instance) {
        if (selectedDates.length > 0) {
            // 获取用户选择的日期
            var selectedDate = selectedDates[0];

            // 格式化日期为MM/DD/YYYY
            var formattedDate = selectedDate.toLocaleDateString('en-US', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit'
            });

            // 构建带日期参数的URL
            var redirectURL = 'https://www.google.com/?date=' + formattedDate;

            // 重定向到新的URL
            window.location.href = redirectURL;
        }
    }
});

请注意,上述代码中的 yourFlatpickrElementId 应该替换为你实际使用的flatpickr日历元素的ID。这段代码将在用户选择日期时重定向浏览器到带有所选日期作为URL参数的新页面。

英文:

I have a web page wherein an element has the flatpickr calendar enabled which is working. Is it possible to modify the flatpickr so that when a user selects a date in the flatpickr, the window will automatically be redirected to a certain page while also bringing in the selected date as a URL parameter? For example when a user selects, say, 06/30/2023 in the flatpickr calendar, they will be taken to "https://www.google.com/?date=06/30/2023".

Is that possible using JavaScript?

答案1

得分: 1

可以修改Flatpickr以实现所需的行为,将用户重定向到带有所选日期作为URL参数的特定页面。试试这样做:

const flatpickrElement = document.getElementById('flatpickr-element');

flatpickr(flatpickrElement, {
  
  // ...
  onValueUpdate: function (selectedDates, dateStr, instance) {
    
    const url = `https://www.google.com/?date=${dateStr}`;

    // 重定向
    window.location.href = url;
  }
});
英文:

it is possible to modify Flatpickr to achieve the desired behavior of redirecting the user to a certain page with the selected date as a URL parameter.
Try..

const flatpickrElement = document.getElementById('flatpickr-element');

flatpickr(flatpickrElement, {
  
  // ...
  onValueUpdate: function (selectedDates, dateStr, instance) {
    
    const url = `https://www.google.com/?date=${dateStr}`;

    // redirect
    window.location.href = url;
  }
});

huangapple
  • 本文由 发表于 2023年6月22日 05:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定