jQuery 如何在文本框中设置默认日期?查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20633982/
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-27 01:14:33  来源:igfitidea点击:

How to set default date in text box? Jquery

jqueryjquery-ui

提问by Rajesh Gurbani

i am using jquery 1.10

我正在使用 jquery 1.10

view Template

查看模板

<script>
    $(function() {
    $( "#departing_on" ).datepicker({
      dateFormat: "dd-mm-yy",
      numberOfMonths:[1,2],
      minDate: new Date() 
    });
    $( "#return_on" ).datepicker({ 
      dateFormat: "dd-mm-yy",
      numberOfMonths:[1,2],
      minDate: new Date() 
    });
    });
</script>

First time while selecting I want default today's date in text box. how ?

第一次在文本框中选择我想要默认今天的日期。如何 ?

回答by Vishal

$("#departing_on").val($.datepicker.formatDate("dd-mm-yy", new Date()));
$("#return_on").val($.datepicker.formatDate("dd-mm-yy", new Date()));

Add the above code at the end of the script. This is required because the datepicker plugin has no provision to set the date in the control while initializing.

在脚本的末尾添加上面的代码。这是必需的,因为 datepicker 插件没有规定在初始化时在控件中设置日期。

回答by Siddharth Srivastva

Not quite sure of what you are asking. You can get current date in JavaScript:

不太确定你在问什么。您可以在 JavaScript 中获取当前日期:

var now = new Date();

If you want to go to current date in jQuery DatePicker, you can apply it on any textfield like this:

如果您想在 jQuery DatePicker 中转到当前日期,您可以将其应用到任何文本字段,如下所示:

$('input.youTextFieldClass').datepicker({ dateFormat: 'mm-dd-yy', 
                                     changeMonth: true,
                                     changeYear: true,
                                     yearRange: '-70:+10',
                                     constrainInput: false,
                                     duration: '',
                                     gotoCurrent: true});

回答by giorgio

just put in the the input field ;)

只需输入输入字段;)

<input type="text" id="departing_on" value="23-03-2013" />

回答by Rajesh Gurbani

The Solution is :

解决方案是:

<input type="text" id="departing_on" class="datepicker form-control input-sm" value='' />

<script type="text/javascript">
function getCurrentDate(){
  today_date = new Date();

  today_Date_Str = ((today_date.getDate() < 10) ? "0" : "") + String(today_date.getDate()) + "-" +((today_date.getMonth() < 9) ? "0" : "") + String(today_date.getMonth() + 1)+ "-" +today_date.getFullYear();

  return today_Date_Str;
}
</script>

<script>
  $(function() {
    $("#departing_on").datepicker({
      dateFormat: "dd-mm-yy",
      numberOfMonths:[1,2],
      minDate: new Date(),
      // gotoCurrent: true
    });
    $("#return_on").datepicker({ 
      dateFormat: "dd-mm-yy",
      numberOfMonths:[1,2],
      minDate: new Date() 
    });
  });
$(function() {
     var date = new Date(); // replace with your date
     $("#departing_on").val(getCurrentDate());
});
</script>