我认为这很简单,但是有没有办法在不循环项目的情况下获取下拉/组合框中值的索引?
最佳答案
我猜您正在谈论选择标签,并且您正在尝试通过选项的值获取选项的索引。如果是这样,您需要执行以下操作:
// Get the select
var select = document.getElementById('mySelect');
// Get the option
var option = select.querySelector('option[value="myValue"]');
// Get the index of that option
var index = Array.prototype.indexOf.call( select.children, option );
关于javascript - 按值获取下拉列表/组合框的索引(客户端)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33712030/