Javascript 表格天数值相对于月份的变化
如何创建一个日期下拉列表,根据在其他下拉列表中选择的月份显示正确的天数 我不希望用户输入出生日期 这可以通过Javascript或Php中的任何内置函数动态完成吗Javascript 表格天数值相对于月份的变化,javascript,forms,Javascript,Forms,如何创建一个日期下拉列表,根据在其他下拉列表中选择的月份显示正确的天数 我不希望用户输入出生日期 这可以通过Javascript或Php中的任何内置函数动态完成吗 或者我必须为每个月编写单独的函数吗?不,没有内置的Javascript或PHP函数来动态操作HTML“月中日”下拉列表 但是,有两种语言的日期函数(·)可以告诉您给定月份的天数 当“月”元素的选择发生变化时,我建议对“日”元素的子元素使用Javascript 或者省去麻烦,使用图书馆 祝你好运 1到28是所有月份的常见值。 对于接下来
或者我必须为每个月编写单独的函数吗?不,没有内置的Javascript或PHP函数来动态操作HTML“月中日”下拉列表 但是,有两种语言的日期函数(·)可以告诉您给定月份的天数 当“月”元素的选择发生变化时,我建议对“日”元素的子元素使用Javascript 或者省去麻烦,使用图书馆
祝你好运 1到28是所有月份的常见值。 对于接下来的29年,我们必须做出选择 787: 如果第二次单击,请删除所有添加的选项,然后重新检查月份和年份值以查找月份所在的类别转到787
<script type="text/javascript">
var monthselected,yearselected,mtype=0,visited=0;
function removeOptions(){ //removes options call each time the day selector is clicked
var x=document.getElementById('dayselect');
while(x.length>29)
{ x.remove(x.length-1);}
}
function addOptions(mtype)
{ //adds options acc to mtype
//alert('initialising');
var i;
var x=document.getElementById("dayselect");
for(i=29;i<=mtype;i++)
{ var option=document.createElement("option");
option.text=i;
try
{//alert('trying');
// for IE earlier than version 8
x.add(option,x.options[null]);
}
catch (e)
{//alert('catching');
x.add(option,null);
}
}
}
function isleapyear(year){ //find if year is leap or not
if((year%4)==0)
{
if((year%100)!=0)
{
return true;
}
else return false;
}
if((year%400)==0)
{
return true;
}
else return false;
}
</script>
<html><td id="bday" >Birthday</td>
<td><select id="month" name="month" class="int" onBlur="monthselected=document.getElementById('month').value;">
<option value="00">Month</option>
<option value="01" >January</option>
<option value="02" > February</option>
<option value="03" > March</option>
<option value="04" > April</option>
<option value="05" > May</option>
<option value="06" > June</option>
<option value="07" > July</option>
<option value="08" > August</option>
<option value="09" > September</option>
<option value="10" > October</option>
<option value="11" > November</option>
<option value="12" > December</option>
</select>
<label id="year-label" class="year int">Year<select class="int" id="year" name="year" selected= onBlur="yearselected=document.getElementById('year').value;"><option value="0" >YYYY</option>
<?php for($i=2012;$i>1912;$i--){echo "<option value=\"$i\">$i</option>";} ?>
</select>
<label id="day-label" class="day int">Day<select id="dayselect" class="int" name="day" onClick=
"if(monthselected==4||monthselected==6||monthselected==9||monthselected==11)
{mtype=30;}
if(monthselected==2&&isleapyear(yearselected))
{mtype=29;}
if(monthselected==2&&!isleapyear(yearselected))
{mtype=28;}
else if(mtype==0){mtype=31;}
if(visited!=0){removeOptions();}
addOptions(mtype);
visited=1;">
<option value="0" > DD</option><?php for($i=1;$i<29;$i++){
echo "<option value=\"$i\">$i</option>";} ?>
</select>
</label>
</td>
var monthselected,yearselected,mtype=0,visited=0;
函数removeOptions(){//在每次单击日期选择器时删除选项调用
var x=document.getElementById('dayselect');
而(x.length>29)
{x.remove(x.length-1);}
}
函数addOptions(mtype)
{//将选项acc添加到mtype
//警报(“初始化”);
var i;
var x=document.getElementById(“dayselect”);
对于(i=29;i
白天
DD
这就是我今天的工作。
在onClick事件中添加或删除了相应的节点。我自由地重新表述了部分内容,以更清楚地说明您想要的内容。如果不是您想要的,请随意回滚尝试。就个人而言,我不喜欢下拉列表中输入dat编号这样简单的内容-为什么不使用简单的文本字段并验证编号用户输入?这与php无关。这其中有一些错误,有时更改不会根据需要进行
<script type="text/javascript">
var monthselected,yearselected,mtype=0,visited=0;
function removeOptions(){ //removes options call each time the day selector is clicked
var x=document.getElementById('dayselect');
while(x.length>29)
{ x.remove(x.length-1);}
}
function addOptions(mtype)
{ //adds options acc to mtype
//alert('initialising');
var i;
var x=document.getElementById("dayselect");
for(i=29;i<=mtype;i++)
{ var option=document.createElement("option");
option.text=i;
try
{//alert('trying');
// for IE earlier than version 8
x.add(option,x.options[null]);
}
catch (e)
{//alert('catching');
x.add(option,null);
}
}
}
function isleapyear(year){ //find if year is leap or not
if((year%4)==0)
{
if((year%100)!=0)
{
return true;
}
else return false;
}
if((year%400)==0)
{
return true;
}
else return false;
}
</script>
<html><td id="bday" >Birthday</td>
<td><select id="month" name="month" class="int" onBlur="monthselected=document.getElementById('month').value;">
<option value="00">Month</option>
<option value="01" >January</option>
<option value="02" > February</option>
<option value="03" > March</option>
<option value="04" > April</option>
<option value="05" > May</option>
<option value="06" > June</option>
<option value="07" > July</option>
<option value="08" > August</option>
<option value="09" > September</option>
<option value="10" > October</option>
<option value="11" > November</option>
<option value="12" > December</option>
</select>
<label id="year-label" class="year int">Year<select class="int" id="year" name="year" selected= onBlur="yearselected=document.getElementById('year').value;"><option value="0" >YYYY</option>
<?php for($i=2012;$i>1912;$i--){echo "<option value=\"$i\">$i</option>";} ?>
</select>
<label id="day-label" class="day int">Day<select id="dayselect" class="int" name="day" onClick=
"if(monthselected==4||monthselected==6||monthselected==9||monthselected==11)
{mtype=30;}
if(monthselected==2&&isleapyear(yearselected))
{mtype=29;}
if(monthselected==2&&!isleapyear(yearselected))
{mtype=28;}
else if(mtype==0){mtype=31;}
if(visited!=0){removeOptions();}
addOptions(mtype);
visited=1;">
<option value="0" > DD</option><?php for($i=1;$i<29;$i++){
echo "<option value=\"$i\">$i</option>";} ?>
</select>
</label>
</td>