How do I simulate placeholder functionality on input date field?

后端 未结 14 1383
生来不讨喜
生来不讨喜 2020-11-27 03:35

It\'s impossible to use placeholder on date fields but I really need it.

I want two date inputs with texts \"From\" and \"To\" on each one as placeholders.

相关标签:
14条回答
  • 2020-11-27 04:24

    I'm using the following CSS only trick:

      input[type="date"]:before {
        content: attr(placeholder) !important;
        color: #aaa;
        margin-right: 0.5em;
      }
      input[type="date"]:focus:before,
      input[type="date"]:valid:before {
        content: "";
      }
    <input type="date" placeholder="Choose a Date" />

    0 讨论(0)
  • 2020-11-27 04:24

    I'm using this css method in order to simulate placeholder on the input date.

    The only thing that need js is to setAttribute of the value, if using React, it works out of the box.

    input[type="date"] {
      position: relative;
    }
    
    input[type="date"]:before {
      content: attr(placeholder);
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: #fff;
      color: rgba(0, 0, 0, 0.65);
      pointer-events: none;
      line-height: 1.5;
      padding: 0 0.5rem;
    }
    
    input[type="date"]:focus:before,
    input[type="date"]:not([value=""]):before
    {
      display: none;
    }
    <input type="date" placeholder="Choose date" value="" onChange="this.setAttribute('value', this.value)" />

    0 讨论(0)
  • 2020-11-27 04:26

    Got the most easiest hack for this problem-use this syntax in your HTML-input-tag

          <input type="text" id="my_element_id" placeholder="select a date" name="my_element_name" onfocus="(this.type='date')" />
          </div>
    
    0 讨论(0)
  • 2020-11-27 04:27

    Try this:

    Add a placeholder attribute to your field with the value you want, then add this jQuery:

    $('[placeholder]').each(function(){
        $(this).val($(this).attr('placeholder'));
      }).focus(function(){
        if ($(this).val() == $(this).attr('placeholder')) { $(this).val(''); }
      }).blur(function(){
        if ($(this).val() == '') { $(this).val($(this).attr('placeholder')); }
      });
    

    I've not tested it for fields that can't take placeholders but you shouldn't need to change anything in the code at all.

    On another note, this code is also a great solution for browsers that don't support the placeholder attribute.

    0 讨论(0)
  • 2020-11-27 04:27

    Using a jQuery (sure you can achieve this with Vanilla. This make sure the date format still the same.

    $(function() {
      $('.sdate').on('focus', function() {
        $(this).attr('type', 'date');
      });
      $('.sdate').on('blur', function() {
        if(!$(this).val()) { // important
          $(this).attr('type', 'text');
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    
    <input type="text" class="sdate" placeholder="From">

    0 讨论(0)
  • 2020-11-27 04:32

    As i mentioned here

    initially set the field type to text.

    on focus change it to type date

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