如何保留文本框的值?

时间:2010-04-20 11:33:15

标签: php javascript

这是我的视图页面上的内容,我想在调用monthfunc()时将值替换为“0”...在视图内容下面给出monthfunc()

<tr>
<td>
<input type="submit" name="monthplus" id="monthplus" onClick="return monthfunc('monthplus');"  value=">>>>>>">
<input type="hidden" name="monthnum" id="monthnum" value="1">
<input type="text" name="monthname" id="monthname" value="0"  value="<? echo $month;?>" >
<input type="submit" name="monthminus" id="monthminus" onClick="return monthfunc('monthminus');" value="<<<<<<">
</td>
</tr>

我的脚本是

function monthfunc(mnth)
{
 if(mnth == 'monthplus')
 {
  var yn = document.getElementById('monthnum').value;
  ynpo = parseInt(yn)+1;
  if(ynpo==13)
  {
   ynpo=1;
  }

 }

 else if(mnth == 'monthminus')
 {
  var yn = document.getElementById('monthnum').value;
  ynpo = parseInt(yn)-1;
  if(ynpo==0)
  {
   ynpo=12;
  }
 }
  if(ynpo ==1)
  {
    document.getElementById('monthname').value = 'january';
    document.getElementById('monthnum').value = ynpo;
    return true;
  }
  else if(ynpo ==2)
  {
    document.getElementById('monthname').value = 'february';
    document.getElementById('monthnum').value = ynpo;
    return true;
  }
  else if(ynpo ==3)
  {
    document.getElementById('monthname').value = 'March';
    document.getElementById('monthnum').value = ynpo;
    return true;
  }
  return false;

}

如何将值替换为1月1月等值。  实际上我可以更改值但不能保留值...  我想保留值,,,如何做到

2 个答案:

答案 0 :(得分:2)

对于月份名称输入,您有两个“值”。删除第一个会发生什么?

此外,您确实需要改进功能的结构。例如,使用以下数组会显着减少代码:

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

答案 1 :(得分:1)

我不确定我是否完全理解你的问题,但通过答案:

HTML代码段中的一个输入元素有两个值属性:

value="0"

value="<? echo $month;?>"

我首先要删除其中一个。