简体   繁体   English

有没有快速的方法来获得 <option>来自a <select>按价值,使用JavaScript?

[英]Is there any fast way to get an <option> from a <select> by value, using JavaScript?

I have a <select>. 我有一个<select>。 Using JavaScript, I need to get a specific <option> from the list of options, and all I know is the value of the option. 使用JavaScript,我需要从选项列表中获取特定的<option>,而我所知道的只是选项的值。 The option may or may not be selected. 可以选择也可以不选择该选项。

Here's the catch: there are thousands of options and I need to do this a few hundred times in a loop. 这是一个问题:有成千上万的选项,我需要在循环中做几百次。 Right now I loop through the "options" array and look for the option I want. 现在我遍历“选项”数组并寻找我想要的选项。 This is too slow (in the sense that on my very fast machine the browser locked up until I killed it after a few minutes). 这太慢了(从某种意义上说,在我的快速机器上,浏览器锁定,直到我在几分钟后将其杀死)。

Is there any faster way to do this? 有没有更快的方法来做到这一点? I'll take browser-specific ways, but of course a DOM-standard way would be nice. 我将采用特定于浏览器的方式,但当然DOM标准的方式会很好。

I'd do it like this: 我这样做:

// first, build a reverse lookup
var optCount      = mySelect.options.length;
var reverseLookup = {};
for (var i = 0; i < optCount; i++)
{
  var option = mySelect.options[i];
  if (!reverseLookup[option.value])
  {
    // use an array to account for multiple options with the same value
    reverseLookup[option.value] = [];
  }
  // store a reference to the DOM element
  reverseLookup[option.value].push(option);
}

// then, use it to find the option
var foundOptions = reverseLookup["Value that you are looking for"];
if (foundOptions && foundOptions.length)
{
  alert(foundOptions[0].id);
}

I would suggest not having thousands of options in your select. 我建议您在选择中没有数千个选项。

Perhaps you could structure your data differently a select with thousands of entries to me seems wrong. 也许你可以用不同的方式构建你的数据一个选项有几千个条目给我似乎是错误的。

Perhaps your app requires this but it would not be typical usage of this element. 也许你的应用程序需要这个,但它不是这个元素的典型用法。

This is Tomalak's answer with a minor speed tweak. 这是Tomalak的一个小调速的答案。 You see a while loop that iterates down is faster than a for loop that iterates up. 您会看到迭代的while循环比迭代的for循环更快。 (I'm lazy so I won't provide the link.) (我很懒,所以我不提供链接。)

var i = mySelect.options.length - 1;
var reverseLookup = {};
while ( i >= 0 )
{
  var option = mySelect.options[i];
  if (!reverseLookup[option.value])
  {
    // use an array to account for multiple options with the same value
    reverseLookup[option.value] = [];
  }
  // store a reference to the DOM element
  reverseLookup[option.value].push(option);
  i--;
}

// then, use it to find the option
var foundOptions = reverseLookup["Value that you are looking for"];
if (foundOptions && foundOptions.length)
{
  alert(foundOptions[0].id);
}

no there isn't, you are doing it really the best way possible. 没有,你做的确是最好的方式。 The only other thing you can try for maybe a quicker look up is to give each of the options an ID tag so that you can look them up as a DOM object instead of looping through children of a DOM object. 您可以尝试的另一件事可能是更快速的查找是为每个选项提供一个ID标记,以便您可以将它们作为DOM对象查找,而不是循环遍历DOM对象的子项。

You could loop through all the options once and put all items into an associative array. 您可以遍历所有选项并将所有项目放入关联数组中。 Then you can just look for myOptions[valueImLookingFor] . 然后你可以找到myOptions[valueImLookingFor]

I haven't tested this and can't guarantee it will be faster/better. 我没有对此进行测试,也无法保证它会更快/更好。 But it should get rid of all those loops. 但它应该摆脱所有这些循环。

Depending on your setup and needs you could also generate a javascript array on the client side and put it in the markup instead of (or in addition to) the select. 根据您的设置和需求,您还可以在客户端生成一个javascript数组,并将其放在标记中而不是(或除了)选择之外。

My suggestion would be to look at a framework/toolkit like Dojo and its way of selecting DOM nodes . 我的建议是查看像Dojo这样的框架/工具包及其选择DOM节点的方法

The toolkit irons out alot of browser inconsistencies and allows you select and manipulate DOM nodes quickly and easily. 该工具包可以解决很多浏览器的不一致问题,并允许您快速轻松地选择和操作DOM节点。

I would think that it may be an indicator that "thousands" of items in a select probably isn't the best user experience. 我认为这可能是一个指标,选择中“数千”的项目可能不是最好的用户体验。 Maybe you should consider trying to limit your dropdowns to several that narrow the results as a user selects them. 也许您应该考虑尝试将下拉菜单限制为几个,以便在用户选择结果时缩小结果范围。

With jQuery something like this could be faster: 使用jQuery这样的事情会更快:

$("#idselect option[value='yourval']")

http://docs.jquery.com/Selectors/attributeEquals#attributevalue http://docs.jquery.com/Selectors/attributeEquals#attributevalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM