如何使用文本框的值在下拉列表中选择一个值 [英] How to select a value in dropdown using the value of textbox
问题描述
大家好,
我正在执行一个简单的示例,该示例使用文本框的值从下拉列表中选择项目.
这个想法是:
如果我有一个下拉列表,则假定值有红色,蓝色,绿色,黄色等.
还有一个简单的html文本框.
现在假设如果我在文本框中输入红色,然后离开文本框,则如果包含红色值项,则下拉列表值应自动选择为红色值.
我已经尝试了很多方法,但是不知道确切的语法.
谢谢
Arvind Thakur
Hello Guys,
I am executing a simple example of selecting the item from the dropdown list by using the value of a textbox.
The idea is:
If I have a dropdown list, suppose with values red, blue, green, yellow and so on.
And a simple html textbox.
Now suppose if I type red into the textbox then leave the textbox then automatically the dropdown list value should be selected to red value if it contains red value item.
I have tried many things in this but don''t know the exact syntax.
thanks
Arvind Thakur
推荐答案
function ChangeColors()
{
var colors = document.getElementById("colors");
var entryColor = document.getElementById("entrybox").value;
for (var i = 0; i < colors.length; i++)
{
if(entryColor == colors[i].innerText)
{
colors[i].selected = true;
}
}
}
尝试通过以下方式找出
try to find Out in the following way
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
<!--
function setDropDownList(elementRef, valueToSetTo)
{
var isFound = false;
for (var i=0; i<elementRef.options.length; i++)
{
if ( elementRef.options[i].value == valueToSetTo )
{
elementRef.options[i].selected = true;
isFound = true;
}
}
if ( isFound == false )
elementRef.options[0].selected = true;
}
function setDatosCGA(strEstadoCarta)
{
setDropDownList(document.getElementById('<%=DropDownList1.ClientID %>'), strEstadoCarta);
}
function Button1_onclick() {
setDatosCGA('cc');
}
// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>aa</asp:ListItem>
<asp:ListItem>bb</asp:ListItem>
<asp:ListItem>cc</asp:ListItem>
<asp:ListItem>dd</asp:ListItem>
<asp:ListItem>ee</asp:ListItem>
</asp:DropDownList>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
</div>
</form>
</body>
这篇关于如何使用文本框的值在下拉列表中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!