简体   繁体   English

当教科书有任何值时,将自动选择下拉选项

[英]When textbook has any value then automatically drop-down option will selected

When textbox has any value then how to automatically drop-down option will selected?当文本框有任何值时,如何自动选择下拉选项?

For Example :例如 :

This is my drop down这是我的下拉菜单

<option>pick a color</option>  
<option value="red">RED</option>
<option value="blue">BLUE</option>
<option value="others">Green</option>

This is text box :这是文本框:

<input type="text" name="color" id="color" />

Question

If any type value is fill in text box, how to select automatically Green in drop down.如果在文本框中填写任何类型值,如何在下拉列表中自动选择绿色。

You could attach input event to the input to track the user change on input then if so select the option you want, check example bellow.您可以将input事件附加到输入以跟踪用户对输入的更改,然后选择您想要的选项,请查看下面的示例。

Hope this helps.希望这可以帮助。


 $('#color').on('input', function() { if($(this).val()!="") $('#colors').val("others"); else $('#colors').val("0"); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="colors"> <option value="0">pick a color</option> <option value="red">RED</option> <option value="blue">BLUE</option> <option value="others">Green</option> </select> <input type="text" name="color" id="color">

If you're using plain JavaScript, you can try this如果你使用纯 JavaScript,你可以试试这个

<select id="selection">
<option>pick a color</option>  
<option value="red">RED</option>
<option value="blue">BLUE</option>
<option value="others">Green</option>
</select>


<input type="text" id="color" oninput="myFunction()">

<script>
function myFunction() {
    document.getElementById("selection").value ='others';
}
</script>

Note : I haven't added the code to handle reseting the selection in case there's no text in the textbox注意:我没有添加代码来处理重置选择的情况,以防文本框中没有文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下拉选择的选项旧值编辑在Laravel中不起作用 - drop-down selected option old value editing is in not working in laravel HTML 选择较短选项时下拉更改大小 - HTML drop-down change size when shorter option selected 选择下拉列表中的值时如何放大? - How to zoom in when a value from a drop-down is selected? 克隆行后,自动从下拉菜单中删除所选选项 - Automatically remove the selected option in drop down when the row is cloned 在下拉列表中选择选项时自动显示结果 - Automatically show result when option is selected in Drop Down List 即使下拉选择具有相同的名称,也如何获取选择的选项 - How to get the selected option only even though the drop-down select has the same name 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 如何有条件地测试下拉列表中的所选选项/值 - How conditionally test the selected option/value from a drop-down list 在下拉列表中选择一个选项时,在表的行上显示输入文本框 - Show input text-box on the row of the table when an option is selected on a drop-down list 从下拉菜单中选择特定选项时如何document.write - How to document.write when specific option selected from drop-down menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM