javascript - 如何定义和访问备用选项名称的数据列表选项

标签 javascript html html-datalist

当从数据列表中选择州名称(即加利福尼亚州)时,我想在结果中显示 option id= 中的州缩写

如何定义和访问 datalist 中的options

例如,如果从datalist中选择California,我想返回结果CA

var input = document.getElementById("myInputId");
input.addEventListener("keyup", function(event) {
  event.preventDefault();
  if (event.keyCode === 13) {
    document.getElementById("myButton").click();
  }
});

function result() {
  document.getElementById("myResult").innerHTML = input.value;
 myObj.info.forEach(function(e, z) {
  var q = document.getElementById("myResult").innerHTML;
  if (e.properties.id == q) {
    document.getElementById("position").innerHTML = z;
    document.getElementById("width").innerHTML =     myObj.info[z].properties.width;

  }

});
}

myObj = {
  "type":"A",
  "info": [
    { "item":"1", "properties":{ "id":"Alabama", "height": "25", width: "50" } },
    { "item":"2", "properties":{ "id":"California", "height": "30", width: "40" } },
    { "item":"3", "properties":{ "id":"Vermont", "height": "20", width: "100" } }
  ]
}
<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>Option ID (State abbreviation):<span id="option"></span></p>
<p>Result:<span id="myResult"></span></p>
<p>Position in object:<span id="position"></span></p>
<p>Width:<span id="width"></span></p>

最佳答案

使用 this.getAttribute('id') 从输入字段获取 ID。

然后类似下面的内容:

document.getElementById('myInput').getElementsByTagName('option')[z].getAttribute('id')

var input = document.getElementById("myInputId");
input.addEventListener("keyup", function(event) {
  event.preventDefault();
  if (event.keyCode === 13) {
    document.getElementById("myButton").click();
  }
});

function result() {
  document.getElementById("myResult").innerHTML = input.value;
 myObj.info.forEach(function(e, z) {
  var q = document.getElementById("myResult").innerHTML;
  if (e.properties.id == q) {
    document.getElementById("position").innerHTML = z;
    document.getElementById("width").innerHTML =     myObj.info[z].properties.width;

document.getElementById("option").innerHTML = document.getElementById('myInput').getElementsByTagName('option')[z].getAttribute('id');

  }

});
}

myObj = {
  "type":"A",
  "info": [
    { "item":"1", "properties":{ "id":"Alabama", "height": "25", width: "50" } },
    { "item":"2", "properties":{ "id":"California", "height": "30", width: "40" } },
    { "item":"3", "properties":{ "id":"Vermont", "height": "20", width: "100" } }
  ]
}
<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>Option ID (State abbreviation):<span id="option"></span></p>
<p>Result:<span id="myResult"></span></p>
<p>Position in object:<span id="position"></span></p>
<p>Width:<span id="width"></span></p>

关于javascript - 如何定义和访问备用选项名称的数据列表选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51182544/

相关文章:

javascript - NodeJS 异步回调未成功?

html - CSS3、HTML 慢动画 'train'

PHP 提交后不在下拉列表中显示更新的值

php - sql 查询在我的网页中不起作用

javascript - 我收到 Uncaught TypeError : Cannot read property 'options' of null even though element id defined

javascript - 是否有与数据列表 <option> 的选择相关的事件?

jquery - 从数据列表中选择项目时触发事件,而不是在键入时触发事件

javascript - 当 html 元素有 contenteditable 时立即显示下划线 ="true"spellcheck ="true"

javascript - 禁用 bulma 中的分页按钮不起作用

javascript - Jquery.get 不使用 ssl