[英]Select-box with textinput
我有以下選擇框:
<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>
不,我不想實現,如果選擇了第一個選項(一個沒有名稱的選項),則會出現一個文本輸入字段,以便有人可以在其中輸入自己的值。
那怎么可能?
將.change()
事件與.toggle()
結合使用:
$('.form-control').change(function(){
$('#idofInput').toggle(this.value === "none");
}).change();
添加輸入框
<input class="form-input" name="value" type="text" style="display:none">
添加變化事件http://api.jquery.com/on/
$(document).on("change", ".form-control", function(){
if ($(this).val()=="none")
$(".form-input").show();
else
$(".form-input").hide();
});
如果使用香草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.