如何使用显示文本设置选择元素的selectedIndex?
如何使用显示文本作为参考来设置选择元素的selectedIndex?
示例:
<input id="AnimalToFind" type="text" />
<select id="Animals">
<option value="0">Chicken</option>
<option value="1">Crocodile</option>
<option value="2">Monkey</option>
</select>
<input type="button" onclick="SelectAnimal()" />
<script type="text/javascript">
function SelectAnimal()
{
//Set selected option of Animals based on AnimalToFind value...
}
</script>
是否有其他方法可以在不使用循环的情况下执行此操作?你知道,我正在考虑内置 JavaScript 代码之类的东西。另外,我不使用 jQuery...
How to set selectedIndex of select element using display text as reference?
Example:
<input id="AnimalToFind" type="text" />
<select id="Animals">
<option value="0">Chicken</option>
<option value="1">Crocodile</option>
<option value="2">Monkey</option>
</select>
<input type="button" onclick="SelectAnimal()" />
<script type="text/javascript">
function SelectAnimal()
{
//Set selected option of Animals based on AnimalToFind value...
}
</script>
Is there any other way to do this without a loop? You know, I'm thinking of a built-in JavaScript code or something. Also, I don't use jQuery...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
Try this:
设置选择标签的 selectedIndex 属性将选择正确的项目。最好不要比较两个值(选项 innerHTML && 动物值),您可以使用 indexOf() 方法或正则表达式来选择正确的选项,无论大小写或存在空格
或使用
。匹配(/正则表达式/)
setting the selectedIndex property of the select tag will choose the correct item. it is a good idea of instead of comparing the two values (options innerHTML && animal value) you can use the indexOf() method or regular expression to select the correct option despite casing or presense of spaces
or using
.match(/regular expression/)
如果你想要没有循环或jquery,你可以使用以下
这是直接的 JavaScript。这适用于当前的网络浏览器。鉴于这个问题的年龄,我不确定这是否会在 2011 年起作用。请注意,使用 css 样式选择器非常强大,可以帮助缩短大量代码。
document.getElementById('Animal').querySelectorAll('option[value="searchterm"]');
在索引对象中,您现在可以执行以下操作:
x[0].索引
If you want this without loops or jquery you could use the following
This is straight up JavaScript. This works for current web browsers. Given the age of the question I am not sure if this would have worked back in 2011. Please note that using css style selectors is extremely powerful and can help shorten a lot of code.
document.getElementById('Animal').querySelectorAll('option[value="searchterm"]');
in the index object you can now do the following:
x[0].index
试试这个:
Try this:
您可以通过此代码设置索引:
但请记住在此实践中要注意,如果您选择下拉列表中先前选择的值,您将无法调用服务器端
onclick
方法。You can set the index by this code :
but remember a caution in this practice, You would not be able to call the server side
onclick
method if you select the previous value selected in the drop down..将 name 属性添加到您的选项:
通过该属性,您可以使用 HTMLOptionsCollection.namedItem("Chicken").value 来设置 select 元素的值。
Add name attribute to your option:
With that you can use the HTMLOptionsCollection.namedItem("Chicken").value to set the value of your select element.
您可以使用 HTMLOptionsCollection.namedItem()
这意味着您必须定义选择选项以具有名称属性并具有显示文本的值。
例如
加利福尼亚州
You can use the HTMLOptionsCollection.namedItem()
That means that you have to define your select options to have a name attribute and have the value of the displayed text.
e.g
California