首页 > 解决方案 > 如何从按钮传递参数?

问题描述

我正在尝试制作一个动态函数,该函数可以从用户输入中获取参数并进行简单的数学运算......而且我总是得到一个参考错误。

我将向您展示我的代码,希望您能理解我的目标

提前感谢<3

/* global window*/

function generate(start, end) {
    "use strict";
    
    start = window.myInput.value;
    end = window.myOutput.value;
    
    var years;
    
    for (years = start; years <= end; years += 1) {
        window.mySelect.innerHTML += '<option>' + years + '</option>';
    }
}
<body>
        <table>
            <tr>
                <td>
                    <input type="text" id="myInput">
                </td>
                <td>
                    <input type="text" id="myOutput">
                </td>
                <td>
                    <button onclick="generate()">Generate!</button>
                </td>
                <td>
                    <select id="mySelect">
                        <option selected>your Years</option>
                    </select>
                </td>
            </tr>
        </table> 
        <script src="script.js"></script>
    </body>

PS:我正在尝试将选项从插入的开始年份添加到 .

标签: javascripthtmlfunctionarguments

解决方案


现在你可以得到指定年份的渐变。对你来说有必要吗?

let myInput = document.querySelector('#myInput');
let myOutput = document.querySelector('#myOutput');
let mySelect = document.querySelector('#mySelect');

function generate() {
    "use strict";
    
    let start = myInput.value;
    let end = myOutput.value;
    
    var years;
    
    for (years = start; years <= end; years++) {
        window.mySelect.innerHTML += '<option>' + years + '</option>';
    }
}
<body>
   
            <tr>
                <td>
                    <input type="text" id="myInput">
                </td>
                <td>
                    <input type="text" id="myOutput">
                </td>
                <td>
                    <button onclick="generate()">Generate!</button>
                </td>
                <td>
                    <select id="mySelect">
                        <option selected>your Years</option>
                    </select>
                </td>
            </tr>
  
        <script src="script.js"></script>
    </body>


推荐阅读