此代码用于根据使用组合框的用户的选定值计算年龄,并且必须在下面提供的文本框中显示年龄。每次用户更改组合框中的选定值时,必须刷新年龄。但我当前的代码并没有显示计算的年龄。
<?php
$month = date("m"); //without leading zero(o)
$year = date("Y"); //four digit format
$day = date("d");
$st_year = "1950"; //Starting Year
$month_names = array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
?>
<form name="Month_Year" id="Month_Year" method="post">
<select name="month" id="month">
<?php
for ($i=1; $i<=12; $i++) {
echo "<option ";
if ($i == $month) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">", $month_names[$i-1], "</option>\n";
}
?>
</select>
<select name="year" id="year">
<?php
for ($i=$st_year; $i<=$year; $i++) {
echo "<option ";
if ($i == $year) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">$i</option>\n";
}
?>
</select>
<select name="day" id="day">
<?php
for ($i=1; $i<=31; $i++) {
echo "<option> ";
if ($i == $day) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">$i</option>\n";
}
?>
</select>
// I used this code to combine the selected value in the combo box.
<?php $Convertdays = $month."/".$day."/".$year;
echo $Convertdays;
?>
<script type="text/javascript">
var birth = new Date( <?php '$Convertdays'?>);
var check = new Date();
var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds;
var ageInDays = (check - birth) / milliDay;
var ageInYears = Math.floor(ageInDays / 365 );
var age = ageInDays / 365 ;
</script>
每次更改组合框的值时,文本框都会显示0。 让我们假设文本框的值是1991年10月19日它应该显示21
答案 0 :(得分:0)
将javascript向上移动并使其成为一个函数,以便您可以引用它
然后添加onChange()以在更改选择元素时触发该功能
最后让函数将您的计算输出到页面上的某个地方
<?php
$month = date("m"); //without leading zero(o)
$year = date("Y"); //four digit format
$day = date("d");
$st_year = "1950"; //Starting Year
$month_names = array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
$Convertdays = $month."/".$day."/".$year;
?>
<script type="text/javascript">
function update() {
var e = document.getElementById("month");
var month = e.options[e.selectedIndex].value;
e = document.getElementById("day");
var day = e.options[e.selectedIndex].value;
e = document.getElementById("year");
var year = e.options[e.selectedIndex].value;
var birthDate = month + "/" + day + "/" + year
var birth = new Date(year,month,day,0,0,0);
var check = new Date();
var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds;
var ageInDays = (check - birth) / milliDay;
var ageInYears = Math.floor(ageInDays / 365 );
var age = parseInt(ageInDays / 365) ;
if (age < 3) {
document.getElementById('Calculated').innerHTML = "You are under 3 years old, why are you on the computer?";
} else {
document.getElementById('Calculated').innerHTML = "You are " + age;
}
}
</script>
Enter your birth Date:
<div style="height:15px;"></div>
<form name="Month_Year" id="Month_Year" method="post">
<select name="month" id="month" style="margin-right: 30px;" onChange="update();">
<?php
for ($i=1; $i<=12; $i++) {
echo "<option ";
if ($i == $month) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">", $month_names[$i-1], "</option>\n";
}
?>
</select>
<select name="day" id="day" style="margin-right: 30px;" onChange="update();">
<?php
for ($i=1; $i<=31; $i++) {
echo "<option ";
if ($i == $day) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">$i</option>\n";
}
?>
</select>
<select name="year" id="year" onChange="update();">
<?php
for ($i=$st_year; $i<=$year; $i++) {
echo "<option ";
if ($i == $year) {
echo "selected=\"selected\" ";
}
echo "value=\"$i\">$i</option>\n";
}
?>
</select>
<br>
</form>
<div id="Calculated">
calculated age goes here<br>
</div>