javascript - 如何从按钮传递参数?
问题描述
我正在尝试制作一个动态函数,该函数可以从用户输入中获取参数并进行简单的数学运算......而且我总是得到一个参考错误。
我将向您展示我的代码,希望您能理解我的目标
提前感谢<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:我正在尝试将选项从插入的开始年份添加到 .
解决方案
现在你可以得到指定年份的渐变。对你来说有必要吗?
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>
推荐阅读
- android - Kotlin - 设置 @Annotations 。函数返回类型?
- ansible - 在寄存器变量列表输出上带有 failed_when 的 ansible 循环
- laravel - 使用 Socialite 的 google 社交登录中的 400 invalid_request 错误
- ios - IOS13:选择保存照片后未调用 UIActivityViewController.completionWithItemsHandler
- java - 无法在appium中初始化驱动,找不到错误。附上截图
- c# - 使用 float.NaN 作为 Dictionary 键是否安全?
- vulkan - Vulkan:渲染到屏幕外帧缓冲区和读取结果时出现问题
- sql-server - 在 SQL Server 2017 中按第一行、最后一行和 2 个表排序
- php - 两个 Laravel 子查询(一个模型两个关系)
- bash - “:exit 0”在bash脚本中是什么意思?