C# 更改文本框文本时如何将下拉列表的选定索引设置为 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/380240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set selected index of dropdown to 0 when text of textbox is changed?
提问by Devashri B.
I am using a dropdown to populate a textbox. But if preferred value is not present in dropdown then user directaly enter value in that textbox.
我正在使用下拉列表来填充文本框。但是如果下拉列表中不存在首选值,则用户直接在该文本框中输入值。
If user selects value from dropdown first and then he don't want that value and he types another text in that textbox then at this time the dropdown should be set to index 0 as user is typing another value.
如果用户首先从下拉列表中选择值,然后他不想要该值并且他在该文本框中键入另一个文本,那么此时下拉列表应设置为索引 0,因为用户正在键入另一个值。
I used textchanged event of textbox but it is not working. Can anyone tell me how to write javascript for this? Thanks in advance.
我使用了文本框的 textchanged 事件,但它不起作用。谁能告诉我如何为此编写javascript?提前致谢。
采纳答案by Mark Carpenter
This should work for you:
这应该适合你:
function ResetDropDown(id) {
document.getElementById(id).selectedIndex = 0;
}
function ResetTextBox(id) {
document.getElementById(id).value = '';
}
<select id="MyDropDown" onchange="ResetTextBox('MyTextBox');">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="MyTextBox" type="text" onkeypress="ResetDropDown('MyDropDown');"/>