gpt4 book ai didi

javascript - Jquery Select Features auto age on Birthday 而不是输入类型日期

转载 作者:行者123 更新时间:2023-11-29 18:05:30 25 4
gpt4 key购买 nike

当用户选择月、日和年时,如何自动计算年龄。我有一个代码可以自动计算年龄。但我想拥有而不是输入。请帮我解决这个问题:(

这是我的输入 type="date"

<input type="text" class="form-control" placeholder="Age" disabled id="age" name="age">

这是我的 Jquery

 $("#dateofbirth").change(function(){
var today = new Date();
var dob = new Date($("#dateofbirth").val());
var age = new Date(today - dob).getFullYear() -1970;
$("#age").val(age);

});

这是 JSFIDDLE Working For Input Type="date"

请帮我把它转换成下面这样的选择

<select class="form-control" id="dob-month">
<option value="0">Month</option>
<option value="January">January</option>
...
</select>

<select class="form-control" id="dob-day">
<option value="0">Day</option>
<option value="1">1</option>
...
</select>

<select name="b2byear" class="form-control col-sm-3" id="dob-year">
<option value="0">Year</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
...
</select>

它会在这里自动计算:

 <input type="text" class="form-control" placeholder="Age" disabled id="age" name="age">

最佳答案

给你 DEMO 。我添加了 class为你的每一个select .请参阅内联评论以获得更详细的解释!

$('.year,.month,.day').on('change',function(){ //call change function on all 3 select dropdowns
var selectedYear=$('.year').find('option:selected').val();//get selected year
var selectedDay=$('.day').find('option:selected').val();//get selected day
var selectedMonth=$('.month').find('option:selected').val(); //get selected month
if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0) //check if all the 3 dropdown values are selected
{
var today = new Date();
var dob=new Date(selectedDay+"/"+selectedMonth+"/"+selectedYear); //convert to a valid date
var age = new Date(today - dob).getFullYear() -1970; //calculate age
$("#age").val(age);//display it
}
})

更新

的一些变化DEMO :

$('.year,.month,.day').on('change',function(){
var selectedYear=$('.year').find('option:selected').val();
var selectedDay=$('.day').find('option:selected').val();
var selectedMonth=$('.month').find('option:selected').val();
if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0)
{
var today = new Date();
var dob=new Date(selectedDay+"/"+selectedMonth+"/"+selectedYear);
var age = today.getFullYear() - dob.getFullYear();//just get it directly here
$("#age").val(age);
}
})

更新 2 DEMO

$('.year,.month,.day').on('change',function(){

var selectedYear=$('.year').find('option:selected').val();
var selectedDay=$('.day').find('option:selected').val();
var selectedMonth=$('.month').find('option:selected').val();
if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0)
{
var today = new Date();
var dob=new Date(selectedYear,(selectedMonth-1),selectedDay); //convert to date in this format
var age = today.getFullYear() - dob.getFullYear();//just get it directly here
$("#age").val(age);
}

})

更新 3 DEMO

The HTML standard for forms appears to be such that disabled input elements do not contribute to the form name/value collection.

因此,您将无法获取禁用字段的值,我建议不要将输入字段设置为 disabled。做到readonly并为其添加一个类,让它感觉起来 disabled添加了一些样式如下:

CSS

.readoly{
color:darkgray;
background-color: rgb(235, 235, 228);
border:1px solid !important;
}

关于javascript - Jquery Select Features auto age on Birthday 而不是输入类型日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com