【问题标题】:Html5 placeholder Issue on all IE所有 IE 上的 Html5 占位符问题
【发布时间】:2013-07-16 19:31:09
【问题描述】:

我似乎对 HTML5 占位符有一个奇怪的问题。 我使用下拉菜单来显示/隐藏 div,在 div 中我的文本字段很少

当我从下拉列表中选择一个选项来显示 div 时,会显示 div 但占位符不在文本字段中。

任何帮助将不胜感激。

The place holder plugin

<script type='text/javascript'>
 $(document).ready(function(){
 $("#customertype").change(function() {
            if ($("#customertype option[value='new']").attr('selected')) {
                   $('#newcustomer').show();

            }
            if ($("#customertype option[value='existingcustomer']").attr('selected')) {
                 $('#newcustomer').hide();
                   $('#existingcustomer').show();   
            }
        });
        });
        </script>

   <!--Start New Customer  -->
            <div id="newcustomer" style="display:none;"> 
              <div id="field"> <span id="sprytextfield5">
                <input class="input address" type="text" name="address" placeholder="Enter Mailing Address" id="address" />
                </span> <span id="sprytextfield6">
                <input class="input" type="text" name="city" placeholder="Enter City" id="city" />
                </span> <span id="sprytextfield7">
                <input class="input" type="text" name="province" placeholder="Enter Province" id="province" />
                </span> <span id="sprytextfield8">
                <input class="input" type="text" name="postalcode" placeholder="Enter Postal Code" id="postalcode" />
                </span> </div>
            </div>
            <!--End New Customer  --> 

【问题讨论】:

    标签: javascript jquery html placeholder


    【解决方案1】:

    IE 不支持占位符,至少到 9 为止。

    https://github.com/madeinstefano/ie-placeholder/blob/master/ie-placeholder.js

    使用以下代码:

        //IE placeholder;
    $(function (){
      if (/MSIE 9|MSIE 8|MSIE 7|MSIE 6/g.test(navigator.userAgent)) {
        function resetPlaceholder() {
          if ($(this).val() === '') {
            $(this).val($(this).attr('placeholder'))
              .attr('data-placeholder', true)
              .addClass('ie-placeholder');
            if ($(this).is(':password')) {
              var field = $('<input />');
              $.each(this.attributes, function (i, attr) {
                if (attr.name !== 'type') {
                  field.attr(attr.name, attr.value);
                }
              });
              field.attr({
                'type': 'text',
                'data-input-password': true,
                'value': $(this).val()
              });
              $(this).replaceWith(field);
            }
          }
        }
    
        $('[placeholder]').each(function () {
          //ie user refresh don't reset input values workaround
          if ($(this).attr('placeholder') !== '' && $(this).attr('placeholder') === $(this).val()){
            $(this).val('');
          }
          resetPlaceholder.call(this);
        });
        $(document).on('focus', '[placeholder]', function () {
          if ($(this).attr('data-placeholder')) {
            $(this).val('').removeAttr('data-placeholder').removeClass('ie-placeholder');
          }
        }).on('blur', '[placeholder]', function () { resetPlaceholder.call(this); });
        $(document).on('focus', '[data-input-password]', function () {
          var field = $('<input />');
          $.each(this.attributes, function (i, attr) {
            if (['type','data-placeholder','data-input-password','value'].indexOf(attr.name) === -1) {
              field.attr(attr.name, attr.value);
            }
          });
          field.attr('type', 'password').on('focus', function () { this.select(); });
          $(this).replaceWith(field);
          field.trigger('focus');
        });
      }
    });
    

    【讨论】:

    • 嘿,感谢您的回复,但是当值被添加到字段时,它不会验证我的来自。
    • 您可以使用 required 属性来验证表单,如下所示:
    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 2012-01-04
    • 2016-03-19
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2014-02-25
    相关资源
    最近更新 更多