HTML5 date picker doesn't show on Safari

前端 未结 6 1866
星月不相逢
星月不相逢 2021-02-02 05:13

Having previously used jQuery date picker, I have now converted some of the date fields in forms on my website to the HTML5 date picker.

On the documentation, it says Sa

相关标签:
6条回答
  • 2021-02-02 05:35

    Taken from http://www.javascriptkit.com/javatutors/createelementcheck2.shtml

    With jQuery and jQuery UI you can create a crossbrowser datepicker that only pops up when the browser doesn't support it natively. If you already have jQuery in your project, simply do:

    var dateClass='.datechk';
    $(document).ready(function ()
    {
      if (document.querySelector(dateClass).type !== 'date')
      {
        var oCSS = document.createElement('link');
        oCSS.type='text/css'; oCSS.rel='stylesheet';
        oCSS.href='//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css';
        oCSS.onload=function()
        {
          var oJS = document.createElement('script');
          oJS.type='text/javascript';
          oJS.src='//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js';
          oJS.onload=function()
          {
            $(dateClass).datepicker();
          }
          document.body.appendChild(oJS);
        }
        document.body.appendChild(oCSS);
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="date" name="datechk" class="datechk">

    0 讨论(0)
  • 2021-02-02 05:45

    For those who using WordPress, there's a quick fix using this plugin date and time picker

    You'll just need to pass the CSS selector in plugin settings, or just pass input[type=date]

    0 讨论(0)
  • 2021-02-02 05:46

    When Safari 15 is released, this "issue" will be obsolete.

    "Starting with Safari TP [TP = Technology Preview] 115, released on Oct 22, 2020, UIs for date, datetime-local and time are supported on macOS and iOS. The month input type is unsupported on macOS, but works on iOS. The week input type is unsupported on macOS and iOS."

    • https://bugs.webkit.org/show_bug.cgi?id=119175#c20
    • https://webkit.org/status/#feature-date-and-time-input-types

    Screenshots:

    • https://twitter.com/stefanjudis/status/1320259247750381568
    • https://twitter.com/foolip/status/1319559899358089216
    0 讨论(0)
  • 2021-02-02 05:52

    You can use a regex pattern to validate the format in the placeholder like in the example above. The one here is a good starting point but you may want to create your own.

    Try the code below in Safari without adding a valid formatted value like a string of text.

    Incorrect format shows a validation error, correct format (dd/mm/yyyy or dd-mm-yyyy) will submit the form, and it disappears.

    <form>
    <input type="date" placeholder="dd/mm/yyyy" pattern="(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)" required>
    <button type="submit">Check if value is valid</button>
    </form>

    0 讨论(0)
  • 2021-02-02 05:55

    Safari does not include a native datepicker for its desktop version (although it does for iOS). Incidentally, neither does IE. It's very frustrating as it could save developers a lot of time if they did.

    This is a useful link for tracking support for it: http://caniuse.com/#feat=input-datetime

    0 讨论(0)
  • 2021-02-02 06:01

    Although there is no native datepicker for Safari (or IE) a pretty good workaround is to add a placeholder attribute to the date input. This informs Safari and IE users which format the fallback text input should be (which is yyyy-mm-dd).

    The placeholder doesn't display on browsers that support type="date" so this workaround won't affect other browsers.

    e.g. <input type="date" placeholder="yyyy-mm-dd" />

    0 讨论(0)
提交回复
热议问题