【问题标题】:How to set selected index of dropdown to 0 when text of textbox is changed?更改文本框文本时如何将下拉列表的选定索引设置为0?
【发布时间】:2008-12-19 06:28:24
【问题描述】:
我正在使用下拉列表来填充文本框。但是,如果下拉列表中不存在首选值,则用户直接在该文本框中输入值。
如果用户首先从下拉列表中选择值,然后他不想要该值,并且在该文本框中键入另一个文本,那么此时下拉列表应设置为索引 0,因为用户正在输入另一个值。
我使用了文本框的 textchanged 事件,但它不起作用。谁能告诉我如何为此编写javascript?
提前致谢。
【问题讨论】:
标签:
c#
javascript
textbox
drop-down-menu
【解决方案1】:
这应该适合你:
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');"/>