[英]How to set selected index of dropdown to 0 when text of textbox is changed?
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. 我使用了文本框的textchanged事件,但它不起作用。 Can anyone tell me how to write javascript for this? 谁能告诉我如何为此编写javascript? Thanks in advance. 提前致谢。
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');"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.