根据输入字段更改文本并根据单选按钮禁用输入

时间:2011-12-05 15:16:23

标签: javascript javascript-events unobtrusive-javascript

需要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 个答案:

答案 0 :(得分:1)

怎么了rami? (马克?)

只需添加一个事件监听器,您可能需要查看此示例: Event Listeners