javascript - 如何将 indexOf 与 datalist 选项一起使用
问题描述
当stateName
从 中选择一个(即佛蒙特州)时datalist
,我想在结果中显示州缩写名称abbrName
(即 VT)datalist
option id=
。
我当前的代码datalist
options
使用从 派生的索引值搜索object
,但问题是datalist
和 的object
排序方式不同,因此索引值z
产生错误的结果。
我应该搜索datalist
withIndexOf
吗?如何找到datalist
索引位置?
var n = document.getElementById("myInputId");
n.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("myButton").click();
}
});
function result() {
var a = n.value;
document.getElementById("stateName").innerHTML = a;
myObj.info.forEach(function(e, z) {
var q = document.getElementById("stateName").innerHTML;
if (e.properties.id == q) {
document.getElementById("statePosition").innerHTML = z;
document.getElementById("statePopulation").innerHTML = myObj.info[z].properties.population;
document.getElementById("abbrName").innerHTML = document.getElementById('myInput').getElementsByTagName('option')[z].getAttribute('id');
}
});
}
myObj = {
"type": "A",
"info": [{
"item": "1",
"properties": {
"id": "Vermont",
"population": "620,000"
}
},
{
"item": "2",
"properties": {
"id": "Alabama",
"population": "4,780,000"
}
},
{
"item": "3",
"properties": {
"id": "California",
"population": "39,540,000"
}
}
]
}
<input list="myInput" id="myInputId" value="" option="">
<button id="myButton" onClick="result()">submit</button>
<datalist id="myInput">
<option id="AL">Alabama</option>
<option id="CA">California</option>
<option id="VT">Vermont</option>
</datalist>
<p>Object index position: <span id="statePosition"></span></p>
<p>Object result (state name): <span id="stateName"></span></p>
<p>Object result (population): <span id="statePopulation"></span></p>
<p>Datalist index position (state abbreviation position): <span id="abbrPosition"></span></p>
<p>Datalist "option id" (state abbreviation name): <span id="abbrName"></span></p>
解决方案
当然,它会产生错误的结果。
您可以将选项和数组的顺序更改为相同索引的相同值。
或者您可以搜索数据列表中的值并将其id
作为abbrName
.
function result() {
var a = n.value;
document.getElementById("stateName").innerHTML = a;
myObj.info.forEach(function (e, z) {
var q = document.getElementById("stateName").innerHTML;
if (e.properties.id == q) {
document.getElementById("statePosition").innerHTML = z;
document.getElementById("statePopulation").innerHTML = myObj.info[z].properties.population;
}
});
document.getElementById("abbrName").innerHTML = [].find.call(document.getElementById('myInput').getElementsByTagName('option'), ({ value }) => value === a).id;
}
它使用类似对象的数组
document.getElementById('myInput').getElementsByTagName('option')
an borrows Array#find
from an array and takes the above array like object as this
objects together with Function#call
.
As find
callback
({ value }) => value === a
a destructuring assignment takes place, where just a single property of the iterating object is taken and then compared with the given value.
If the comparision returns true
, the object is found and returned from find
. Then it takes the id
propery.
推荐阅读
- asp.net-mvc - 请求在 Postman 上返回 200,但在网站上进行 404 测试
- python-3.x - 如果 selenium 无头运行,Cron 不会从 python 脚本输出任何内容
- java - 使用泛型类型实现接口
- akka - Akka 流:Alpakka 不使用多个 CPU 内核
- c++ - 计算从 1 到 n 的总设置位
- javascript - 如何在 ASP Classic (javascript) 中循环或访问对象集合?
- ssl - 如何在 GitLab-ci.yml 中运行 curl --retry 命令
- java - fhir.executeBundle 替换资源 id...如何防止这种情况?
- android - Android 在后台录制视频
- node.js - npm 错误!代码 EMFILE npm ERR!系统调用产生 git npm ERR!路径 git npm 错误!errno EMFILE npm 错误!生成 git EMFILE