jquery ui 日期选择器在文本框中具有默认文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25190278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:04:08  来源:igfitidea点击:

jquery ui date picker with default text in textbox

jqueryjquery-ui-datepicker

提问by user1697113

is there any way we can have a default text value (not some date but some sort of label, which is displayed inside textbox, but gets changed to selected date if the user selects some date.

有什么办法可以让我们有一个默认的文本值(不是某个日期,而是某种标签,显示在文本框中,但如果用户选择某个日期,则会更改为选定日期。

// I want the MM/DD/YYYY to be displayed inside text box if no date is selected by user

    <input id="datePicker" value ="MM/DD/YYYY">
    $(function() { 
    $( "#datepicker" ).datepicker( )});

I tried adding a dummy input box with mm/dd/yyyy and showing and hiding the same using focus and blur methods, but its not working, properly. Is there any elegant way to achieve this? Any help is appreciated. PS: I am not using html5 and i need this thing to be working in ie 8 ,9. it should look something like this text box sample

我尝试添加一个带有 mm/dd/yyyy 的虚拟输入框,并使用焦点和模糊方法显示和隐藏相同的输入框,但它无法正常工作。有没有什么优雅的方法来实现这一目标?任何帮助表示赞赏。PS:我没有使用 html5,我需要这个东西在 ie 8 ,9 中工作。它应该看起来像这样 文本框示例

回答by Lenny

You can add placeholder="MM/DD/YYYY"inside your <input>,

您可以placeholder="MM/DD/YYYY"在您的<input>,

<input id="datePicker" placeholder="MM/DD/YYYY">

<input id="datePicker" placeholder="MM/DD/YYYY">

回答by Allen King

Add this inside script tags of the HTML page (or onload). This will show current date in the input box. You may use title="Enter MM/DD/YYYY"in the input to show tooltip. If you really want to show MM/DD/YYYY in the text, you may use css background image property. I am not good at css, but I guess it will work (just create one small png with text MM/DD/YYYY.

将此添加到 HTML 页面(或 onload)的脚本标记中。这将在输入框中显示当前日期。您可以title="Enter MM/DD/YYYY"在输入中使用以显示工具提示。如果你真的想在文本中显示 MM/DD/YYYY,你可以使用 css background image 属性。我不擅长 css,但我想它会起作用(只需创建一个带有文本 MM/DD/YYYY 的小 png。

Date.prototype.formatMMDDYYYY = function(){
        return this.getMonth() + 
        "/" +  this.getDate() +
        "/" +  this.getFullYear();
    }

    var dt = new Date();
    $('#datePicker').val(dt.formatMMDDYYYY());


        $(function() { 
        $( "#datePicker" ).datepicker( )});

回答by magicmanam

If you want to have unified datepickers' behaviour in all your site, simply override datepicker()function:

如果您想在所有站点中统一日期选择器的行为,只需覆盖datepicker()函数:

$(function () {
    var pickerFn = $.fn.datepicker,
        placeholder = 'MM/DD/YYYY';

    $.fn.datepicker = function () {
        var datePicker = pickerFn.apply(this, arguments);  

        var self = this;
        self.attr('placeholder', placeholder);

        return datePicker;
    };
});

回答by dtmiRRor

<input id="datePicker" placeholder="MM/DD/YYYY">

<script type="text/javascript">
$(function() {
  $('#datePicker').daterangepicker({
      autoUpdateInput: false
 });
</script>