[英]Select-box with textinput
I have the following Select-Box: 我有以下选择框:
<select class="form-control" id="a-tenantID2">
<option value="">Only Tenant</option>
<optgroup label="editor">
<option value="none"></option>
<option value="test1">test1</option>
</optgroup>
</option>
</select>
No I want to achieve, that If the first option is selected (the one without name), there appears an text-input field, so someone can write his own value into it. 不,我不想实现,如果选择了第一个选项(一个没有名称的选项),则会出现一个文本输入字段,以便有人可以在其中输入自己的值。
How is that possible? 那怎么可能?
Do something with .change()
event in conjunction with .toggle()
: 将
.change()
事件与.toggle()
结合使用:
$('.form-control').change(function(){
$('#idofInput').toggle(this.value === "none");
}).change();
Add an input box 添加输入框
<input class="form-input" name="value" type="text" style="display:none">
Add an on-change event http://api.jquery.com/on/ 添加变化事件http://api.jquery.com/on/
$(document).on("change", ".form-control", function(){
if ($(this).val()=="none")
$(".form-input").show();
else
$(".form-input").hide();
});
If using vanilla Javascript: 如果使用香草Javascript:
document.getElementById('a-tenantID2').addEventListener('change', function (e) { document.getElementById(this.id + '_input').classList[this.value !== 'none' ? 'add' : 'remove']('hidden'); });
.hidden { display: none; }
<select class="form-control" id="a-tenantID2"> <option value="">Only Tenant</option> <optgroup label="editor"> <option value="none"></option> <option value="test1">test1</option> </optgroup> </select> <input type="text" id="a-tenantID2_input" class="hidden" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.