How to make <input type=“date”> supported on all browsers? Any alternatives?

前端 未结 11 2040
不知归路
不知归路 2020-11-29 16:57

I\'m working with HTML5 elements input attributes and only Google Chrome supports the date, time attributes. I tried Modernizr but I can\'t understand on how to integrate it

11条回答
  •  渐次进展
    2020-11-29 17:45

    I was having problems with this, maintaining the UK dd/mm/yyyy format, I initially used the answer from adeneo https://stackoverflow.com/a/18021130/243905 but that didnt work in safari for me so changed to this, which as far as I can tell works all over - using the jquery-ui datepicker, jquery validation.

    if ($('[type="date"]').prop('type') !== 'date') {
        //for reloading/displaying the ISO format back into input again
        var val = $('[type="date"]').each(function () {
            var val = $(this).val();
            if (val !== undefined && val.indexOf('-') > 0) {
                var arr = val.split('-');
                $(this).val(arr[2] + '/' + arr[1] + '/' + arr[0]);
            }
        });
    
        //add in the datepicker
        $('[type="date"]').datepicker(datapickeroptions);
    
        //stops the invalid date when validated in safari
        jQuery.validator.methods["date"] = function (value, element) {
            var shortDateFormat = "dd/mm/yy";
            var res = true;
            try {
                $.datepicker.parseDate(shortDateFormat, value);
            } catch (error) {
                res = false;
            }
            return res;
        }
    }
    

提交回复
热议问题