【发布时间】:2019-05-28 07:31:08
【问题描述】:
我有一个设备数组,每个设备都有自己唯一的 id,我希望创建一个 searchBox 来过滤这个 id。到目前为止,我对它进行了部分管理,因此它会检查输入中的字符是否与设备 ID 中的字符匹配。但是不是我想要的,下面的例子:
id =35678; input.value = 5
它不应该工作,因为第一个字符是3
id = 35679;输入值= 35
第一个字符应该是一样的
问题在于匹配/包含函数,但不知道如何替换它以使其工作
input_box.addEventListener('keyup', function(){
var search_result = this.value;
var device = document.querySelectorAll('[device_id]')
var array_of_devices = [];
for (var i = 0; i < device.length; i++) {
array_of_devices.push(device[i].getAttribute('device_id'))
}
array_of_devices.forEach(el => {
if (!el.match(search_result)) {
var not_matched = document.querySelector(`[device_id="${el}"]`)
not_matched.style.display = "none"
} else {
var matched = document.querySelector(`[device_id="${el}"]`)
matched.style.display = "block"
}
})
})
【问题讨论】:
标签: javascript arrays filter match