如何更改 Chrome 中日期字段中显示的 HTML5 占位符文本 [英] How do I change the HTML5 placeholder text that appears in date field in Chrome

查看:19
本文介绍了如何更改 Chrome 中日期字段中显示的 HTML5 占位符文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我惊喜地发现以下代码在 Chrome 中查看时呈现日期选择器:

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

<input type="date" name="wdate" id="wdate" value="" placeholder="date of purchase" />

但是,占位符文本被忽略,而是显示日期/月/年".

However, the placeholder text is ignored and "Date/Month/Year" is shown instead.

如何更改占位符文本并使用我自己的?

How can I change the placeholder text and use my own?

我已经试过了:

<script>
    $("#wdate").attr("placeholder", "Day/Month/Year of purchase");
</script>

但无济于事.

推荐答案

较旧的问题,但这里是我最近不得不使用的解决方案.

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

您将无法使用 HTML5 日期字段,但这可以通过 jQuery UI 日期选择器来完成.http://jsfiddle.net/egeis/3ow88r6u/

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>

这篇关于如何更改 Chrome 中日期字段中显示的 HTML5 占位符文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆