Javascript 如何使用 jQuery 从 HTML <input type="date"> 中提取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33312318/
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
How to extract values from HTML <input type="date"> using jQuery
提问by jmancherje
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.
使用 HTMLinput type="date"
和提交按钮。我想用日期输入中的适当值填充变量day
,month
和year
。
<input type="date" id="date-input" required />
<button id="submit">Submit</button>
and the jQuery:
和 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/
这是一个代码示例:https: //jsfiddle.net/dkxy46ha/
the console error is telling me that .getDate()
is not a function.
控制台错误告诉我这.getDate()
不是一个函数。
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
我见过类似的问题,但解决方案对我不起作用。如何从input type="date"
? 中提取日、月和年?谢谢
回答by Yuriy Yakym
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.
首先,您需要Date
从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('/'));
});
Working example: https://jsfiddle.net/8poLtqvp/
回答by guest271314
Date value returned by input type="date"
is format yyyy-mm-dd
. Could use .split()
with argument "-"
to retrieve array containing [yyyy, mm, dd]
返回的日期值为input type="date"
format yyyy-mm-dd
。可以.split()
与参数"-"
一起使用来检索包含的数组[yyyy, mm, dd]
Note, alert()
expects string as parameter ; does not print values same as console.log()
with comma ,
operator
注意,alert()
需要字符串作为参数;不打印console.log()
与逗号,
运算符相同的值
var day, month, year;
$('#submit').on('click', function() {
var date = $('#date-input').val().split("-");
console.log(date, $('#date-input').val())
day = date[2];
month = date[1];
year = date[0];
alert(day + month + year);
});
jsfiddle https://jsfiddle.net/dkxy46ha/2/
jsfiddle https://jsfiddle.net/dkxy46ha/2/
回答by Oleg Sklyar
date = new Date($('#date-input').val())
date.getDate()
...
...
回答by pramod gupta
<input type="date" id="date-input"/>
<input type="submit" id="submit" value="submit"/>
<script>
$('#submit').on('click', function(){
var date = new Date($('#date-input').val());
var day = $('#date-input').getDate();
var month = $('#date-input').getMonth() + 1;
var year = $('#date-input').getFullYear();
alert(day+"/"+ month+"/"+year);
});
</script>
Date class will convert input data into date type. Then getMonth() gives us value from 0-11 so we can either consider January as 0 or we can add 1 to received value that's what I did here.
日期类将输入数据转换为日期类型。然后 getMonth() 为我们提供 0-11 的值,因此我们可以将一月视为 0,或者我们可以将收到的值加 1,这就是我在这里所做的。
Here I just used Jquery to fetch date, months and year respectively and if you want to post the data you can convert that values into string.
在这里,我只是使用 Jquery 分别获取日期、月份和年份,如果您想发布数据,您可以将这些值转换为字符串。