简体   繁体   English

如何在文本框文本更改时将下拉列表的选定索引设置为0?

[英]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.

相关问题 当组合框选定索引更改时,填充文本框 - Filling Textbox, when combobox Selected Index Changed 在C#Windows窗体中自动填充文本框选定的索引更改时填充文本框 - Filling textbox when autofill text box selected index changed in C# windows form 在组合框中更改所选索引后,如何将此新索引添加到另一个文本框中? - When selected index changed in combo box, how to add this new index to another text box? 在下拉列表中选定的索引已更改,我想在gridview中创建动态空文本框 - in dropdown selected index changed i want to create Dynamic empty textbox inside gridview 设置下拉菜单的选定文本 - Set selected text for DropDown 下拉列表选择索引改变了剃刀 - dropdown list selected index changed razor 在另一文本框中更改文本时,如何在asp.net中的一个文本框中获取要更改的文本 - how to get text to be changed in one textbox in asp.net when text changed in other textbox 更改选定的下拉菜单项后如何重定向到另一个页面? - How to redirect to another page when selected dropdown item is changed? 如何在页面更改时保留下拉列表中的选定值? - How to keep the selected value from the dropdown when the page is changed? 在C#中更改下拉索引时清除“更新面板”中的TextBox值 - Clearing the TextBox values in Update Panel when dropdown index changed in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM