简体   繁体   English

除非选择选择器选项,否则禁用文本输入

[英]Disable text input unless a selector option is chosen

I have a selector with a list of options. 我有一个带有选项列表的选择器。 One of the options is 'other' in which case the user may enter their own option in a textbox below. 选项之一是“其他”,在这种情况下,用户可以在下面的文本框中输入自己的选项。

How can I make the textbox disabled and greyed out only to become active when the 'other' option is selected? 如何在选择“其他”选项时使文本框禁用并变灰以使其变为活动状态?

<select id = "list" name="Optionlist">
    <option value = "1">Option one</option>
    <option value = "2">Option two</option>
    <option value = "3">Option three</option>
    <option value = "4">Other</option>
</select>
<br><br>
<label for="Other">Other: </label>
<input type="text" name="Other" id="otherbox"/>

Maybe a little something like this: 也许是这样的:

 $(document).ready(function() { $("#list").change(function() { $("#otherbox").prop("disabled", this.value != "4"); }).change(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id = "list" name="Optionlist"> <option value = "1">Option one</option> <option value = "2">Option two</option> <option value = "3">Option three</option> <option value = "4">Other</option> </select> <br><br> <label for="Other">Other: </label> <input type="text" name="Other" id="otherbox"/> 

 $('#list').change(function() {//on change of select $('#otherbox').prop('disabled', $('option:selected', this).val() != '4');//check if value is not other then disable }).change();//call on change manually so on load will run the change event 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="list" name="Optionlist"> <option value="1">Option one</option> <option value="2">Option two</option> <option value="3">Option three</option> <option value="4">Other</option> </select> <br> <br> <label for="Other">Other:</label> <input type="text" name="Other" id="otherbox" /> 

 <html> <head> <body> <select onchange="func(this)" id = "list" name="Optionlist"> <option value = "1">Option one</option> <option value = "2">Option two</option> <option value = "3">Option three</option> <option value = "4">Other</option> </select> <br><br> <label for="Other">Other: </label> <input type="text" name="Other" id="otherbox" disabled/> <script> function func(obj){ document.getElementById('otherbox').setAttribute('disabled',true); if(obj.value == 4) document.getElementById('otherbox').removeAttribute('disabled'); } </script> </body> </html> 

An implementation without Jquery 没有Jquery的实现

暂无
暂无

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

相关问题 使用选择器使用jquery启用/禁用输入文本 - using selector to enable/disable input text with jquery 根据选择的内容将文本输入或选择选项发布到数据库中 - Posting a Text input or select option to the database depending on which one is chosen 除非填写所有文本输入字段,否则禁用表单按钮 - Disable form button unless all text input fields are filled in 在双下拉选择选项中的选择选项时出现输入文字 - Having a input text appear when chosen option in a double drop down select option 尝试这样做,如果选择了下拉列表中的第三个选项,则文本框将禁用并出现一个新框 - Trying to make it so if the 3rd option on the dropdown is chosen, then the text boxes will disable and a new box will appear 在选定的搜索中禁用某些选项 - Disable some option from search in chosen 在JavaScript中禁用文本选择器 - Disable Text Selector In JavaScript AngularJS通过文本输入过滤对象数组,具体取决于所选的选项 - AngularJS filter an array of objects by text input depending on what select option is chosen 一旦用户在 AutoComplete 输入类型文本上选择了一个选项,我需要使用 JavaScript 更改 disabled 属性吗? - I need to change the disabled attribute with JavaScript once the user has chosen an option on the AutoComplete input type text? 如果未从下拉菜单中选择选项,如何禁用拖动选项? - How to disable drag option if option is not chosen from dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM