2011-12-05 56 views
0

需要JS幫助,根據輸入字段更改文本並根據單選按鈕禁用輸入

我需要根據輸入字段中輸入的「年份」更改輸入的文本。

並且在更改「要顯示的年限」時,禁用不必要的收入/資產字段。

<!DOCTYPE html> 
<html> 
<head> 
    <title>title</title> 
    <script type="text/javascript"> 
     window.onload=function() { 
      // On "year" change, validate it and cahnge the text befor the input fields. 
      var year = document.getElementsByName("year"); 
      year.onclick=function() { 
       this.year0_text.value = year[this.value]; 
       this.year1_text.value = year[this.value]+1; 
       this.year2_text.value = year[this.value]+2; 
      } 

      // and on "years" change, disable the unnecessary income/assets fields. 
     } 
    </script> 
</head> 
<body> 
    <form name="ar" action="." method="post"> 
     Year:<br /> 
     <input type="number" name="year" value="2011"><br /> 
     <br /> 
     Years to Show:<br /> 
     <input type="radio" name="years" value="0.5">first period (not full year)<br /> 
     <input type="radio" name="years" value="1">1 year<br /> 
     <input type="radio" name="years" value="2">2 years<br /> 
     <input type="radio" name="years" value="3">3 years<br /> 
     <br /> 
     Income:<br /> 
     <span class="year0_text">20XX-0</span> <input type="number" name="year0_income" value="0"><br /> 
     <span class="year1_text">20XX-1</span> <input type="number" name="year1_income" value="0"><br /> 
     <span class="year2_text">20XX-2</span> <input type="number" name="year2_income" value="0"><br /> 
     <br /> 
     Assets:<br /> 
     <span class="year0_text">20XX-0</span> <input type="number" name="year0_assets" value="0"><br /> 
     <span class="year1_text">20XX-1</span> <input type="number" name="year1_assets" value="0"><br /> 
     <span class="year2_text">20XX-2</span> <input type="number" name="year2_assets" value="0"><br /> 
    </form> 
</body> 
</html> 

DEMO - http://jsfiddle.net/92GaE/2/

回答

1

怎麼了支? (?馬科雷)

只需添加一個事件監聽器,你可能想看看這個例如: Event Listeners

相關問題