javascript - 可以在 array.from 中返回元素 ID?
问题描述
是否可以使用 Array.from 循环遍历值,然后返回该特定元素的 ID?就像我下面的代码:
var inputfields = form.querySelectorAll('input[type=text]');
inputfields.forEach(field => field.addEventListener('input', setTextValue));
function setTextValue() {
textValue = Array.from(inputfields).map(e => e !== '' ? e.value : null).filter(Boolean).join(", ");
showValues();
}
我希望我的函数 setTextValue 可以循环遍历 的值,e
然后返回该元素的 ID 而不是该值。
解决方案
你是这个意思吗?
var inputfields = form.querySelectorAll('input[type=text]');
inputfields.forEach(field => field.addEventListener('input', setTextValue));
function setTextValue() {
textValue = Array.from(inputfields)
.filter(e => !!e.value) // Remove the elements without values
.map(e => e.id) // Get the id for each element
.join(", "); // comma separated ids
showValues();
}
推荐阅读
- laravel - Laravel Nova - 获取已保存资源的 ID
- javascript - 如何在弹出窗口中调用 javascript 命令?
- splunk - 为传递搜索字符串中的字段的 csv 文件的每一行运行子查询
- excel - .Range().Find() 在使用字符串对象时抛出“需要对象”错误
- jenkins - 使用 Kustomize 部署现有作业
- vue.js - ELEMENT IO 全选按钮还选择不同列中的复选框
- c# - ASP.NET Core:HtmlHelper 扩展(迁移问题)
- node.js - 无法使用 MongoDB 和 Node.js 在循环内迭代异步函数
- c# - 应用程序配置文件未保存
- java - java中是否有任何方法可以检查字符串是否为数字?