javascript - 基于 Javascript 中的用户输入获取输入表单的值和 id 的函数
问题描述
我有几个输入。当用户在输入中输入内容时,我想使用函数获取其值和 id。最好的方法是什么?
<input type="number" class="form-control" id="id1">
<input type="number" class="form-control" id="id2">
.
.
<input type="number" class="form-control" id="id9">
<input type="number" class="form-control" id="id10">
<script>
function getIdVal(){
// how to get those values and ids from this function?
}
</script>
解决方案
使用change/keyup/keydown
事件(如果需要,可以替换为另一个事件),您可以执行以下操作:
<input type="number" class="form-control" onchange="run(this)" id="id1">
<input type="number" class="form-control" onchange="run(this)" id="id2">
<input type="number" class="form-control" onchange="run(this)" id="id9">
<input type="number" class="form-control" onchange="run(this)" id="id10">
而对于你的 JS
function run(ele){
var id = ele.id;
var value = ele.value;
console.log(id, value);
}
这是一个JSFiddle。
推荐阅读
- kubernetes - 连接测试在 netcat 上工作但不在 telnet 上
- interface - 苹果手表应用程序上的黑屏页面超过一页
- javascript - 在javascript中组合2张图片
- php - 如何在 PHP 等待用户输入时定期调用函数?
- mysql - 如何使用条件语句在 SQL 中排序
- c++ - 为什么 vector::erase 似乎会导致崩溃?
- c - c中的以下代码是否会产生内存泄漏?
- audio - 使用软件或命令行从音频/视频中删除人声
- html - 使用 Angular 8 的 Ionic 框架 - 需要基本 HTML 表单,最小,最大验证不起作用
- gnuplot - Gnuplot——向xaxis添加太多数据