如何从HTML中提取值< input type =" date">使用jQuery [英] How to extract values from HTML <input type="date"> using jQuery

查看:279
本文介绍了如何从HTML中提取值< input type =" date">使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HTML input type =date和一个提交按钮。我想填充变量 day month year

 < input type =dateid =date-input必需/> 
< button id =submit>提交< / button>

和jQuery:

  var日,月,年; $('#submit')。on('click',function(){
day = $('#date-input')。getDate();
getMullth()+ 1;
year = $('#date-input')。getFullYear();
alert(day,month,year) ;
});

下面是一个代码示例:
https://jsfiddle.net/dkxy46ha/



控制台错误告诉我 .getDate()不是一个函数。



我曾经见过类似的问题,但解决方案并不适合我。我如何从 input type =date中提取日,月和年?首先你需要从<$创建一个 Date 对象 c $ c> input 元素值。然后你可以从这个对象中获得日,月和年。

  $('#submit')。on ('click',function(){
var date = new Date($('#date-input')。val());
day = date.getDate();
month = date.getMonth()+ 1;
year = date.getFullYear();
alert([day,month,year] .join('/'));
}) ;

工作示例: https://jsfiddle.net/8poLtqvp/


Using an HTML input type="date" and a submit button. I would like to populate the variables day, month, and year with the appropriate values from the date input.

<input type="date" id="date-input" required />
<button id="submit">Submit</button>

and the jQuery:

var day, month, year;

$('#submit').on('click', function(){
  day = $('#date-input').getDate();
  month = $('#date-input').getMonth() + 1;
  year = $('#date-input').getFullYear();
  alert(day, month, year);
});

Here's a code sample: https://jsfiddle.net/dkxy46ha/

the console error is telling me that .getDate() is not a function.

I have seen similar questions but the solutions have not worked for me. How can I extract the day, month and year from the input type="date"? Thanks

解决方案

Firstly you need to create a Date object from input element value. And then you will be able to get day, month and year from this object.

$('#submit').on('click', function(){
  var date = new Date($('#date-input').val());
  day = date.getDate();
  month = date.getMonth() + 1;
  year = date.getFullYear();
  alert([day, month, year].join('/'));
});

Working example: https://jsfiddle.net/8poLtqvp/

这篇关于如何从HTML中提取值&lt; input type =&quot; date&quot;&gt;使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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