简体   繁体   English

如何在jQuery中输入input = date中选择的日期?

[英]How to get the date selected in input type=date in jQuery?

I have an HTML code like this one: 我有一个像这样的HTML代码:

<form action="#" method="get">
    <input type="date">
</form>

I want to have an event or something that will check when the date is changed, and get the date in JS Date object? 我希望有一个事件或什么会检查日期何时更改,并在JS Date对象中获取日期?

Demo 演示

Try change event handler on the input , 尝试在inputchange事件处理程序,

$(document).ready(function(){
    $('input[type="date"]').change(function(){
        alert(this.value);         //Date in full format alert(new Date(this.value));
        var inputDate = new Date(this.value);
    });
});

Attach the change event handler and convert the value into Date : 附加change事件处理程序并将值转换为Date

$("input").on("change", function () {
    var myDate = new Date($(this).val());
    console.log(myDate, myDate.getTime());
});

JSFIDDLE 的jsfiddle

You need to use a .change() jquery handler it will look something like this: 你需要使用.change()jquery处理程序,它看起来像这样:

$(document).ready(function(){
  $('#dateInput').change(function(){
       console.log('Updated Date');
  });
});

<form>
   <input id="dateInput" type="date" />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript从输入类型为date的html元素中获取选定日期 - How to get selected date from html element where input type=date using javascript 如何从jquery datepicker获取选定的日期 - How to get the selected date from jquery datepicker 如何从输入类型的 Jquery/javascript 中的日期获取月份的日期 - How can i get the date of month from a date in Jquery/javascript from input type 如何使用 jquery 从输入类型 date 中提取日期值? - How to extract the date value from input type date using jquery? 如何获得<input type="date"> TypeScript 中的值 - How to get <input type=date> value in TypeScript 使用输入类型日期更改在 jquery 中选择特定日期时的下拉值 - Change the dropdown value when particulare date is selected in jquery using input type date 如何在 input type=“date” HTML 标签中设置选定日期的样式? - How to style selected days in input type=“date” HTML tag? 我如何从中获得价值 <input type=date> jQuery DataTables中的单元格内部? - How do I get the value from <input type=date> inside a cell in jQuery DataTables? 如何在第二个日期输入文本框中禁用jQuery UI中已选择的日期 - How to disable already selected date in jquery ui in second date input textbox 如何从 HTML 中提取值<input type="date">使用 jQuery - How to extract values from HTML <input type="date"> using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM