简体   繁体   中英

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() :

$('.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/

$(document).on("change", ".form-control", function(){
    if ($(this).val()=="none")
        $(".form-input").show();
    else 
        $(".form-input").hide();
});

If using vanilla 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" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM