How do I change the HTML5 placeholder text that appears in date field in Chrome

前端 未结 2 1410
梦如初夏
梦如初夏 2021-01-18 11:09

I was pleasantly surprised to discover that the following code, when viewed in Chrome, renders a datepicker:



        
相关标签:
2条回答
  • 2021-01-18 11:35

    Older question, but here is the solution I recently had to use.

    You will not be able to use the HTML5 date field, but this can be done with jQuery UI datepicker. http://jsfiddle.net/egeis/3ow88r6u/

    var $datePicker = $(".datepicker");
    
    // We don't want the val method to include placehoder value, so removing it here.
    var $valFn = $.fn.val;
    $.fn.extend({
        val: function() {
            var valCatch = $valFn.apply(this, arguments);
            var placeholder = $(this).attr("placeholder");
            
            // To check this val is called to set value and the val is for datePicker element 
            if (!arguments.length && this.hasClass('hasDatepicker')) {
                if (valCatch.indexOf(placeholder) != -1) {
                    return valCatch.replace(placeholder, "");
                }
            }
            return valCatch;
        }
    });
    
    // Insert placeholder as prefix in the value, when user makes a change.
    $datePicker.datepicker({
        onSelect: function(arg) {
            $(this).val($(this).attr("placeholder") + arg);
        }
    });
    
    // Display value of datepicker
    $("#Button1").click(function() {
        alert('call val(): {' + $datePicker.val() + '}');
    });
    
    // Submit
    $('form').on('submit', function() {
        $datePicker.val($datePicker.val());
        alert('value on submit: {' + $datePicker[0].value + '}');
        return false;
    });
    .ui-datepicker { width:210px !important; }
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <form method="post">
        <p>HTML5 Date: <input type="date" placeholder="html5 date:" /></p>
        <p>jQuery DatePicker: <input type="text" class="datepicker" placeholder="Start date:" /></p>
        <p><input id="Button1" type="button" value="Get DatePicker Value" title="Get DatePicker Value" /></p>
        <p><input type="submit" text="fire submit" /></p>
        <p>Original Source for this <a target="_blank" href="http://jqfaq.com/how-to-add-some-custom-text-to-the-datepickers-text-field/">JQFaq Question</a></p>
    </form>

    0 讨论(0)
  • 2021-01-18 11:39

    The date type doesn't support placeholder attribute. Also, the WebKit implementation says it's fixed.

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