Adding to a HTML text field by clicking items in a multiple select box

后端 未结 4 457
[愿得一人]
[愿得一人] 2021-01-26 19:31

I\'m wondering if it is possible that when I click on an item in a multiple select box in HTML that it goes into another form box? Basically, when I click on something I want th

4条回答
  •  盖世英雄少女心
    2021-01-26 19:59

    This will work, with plain javascript:

    var sel = document.getElementsByName ('tags')[0];
    sel.onclick = function () {
        document.getElementsByName ('taginput')[0].value = this.value;
    }
    

    Demo here

    A second version avoiding duplicates:

    var sel = document.getElementsByName('tags')[0];
    var choosen = [];
    sel.onclick = function () {
        var is_there = !!~choosen.indexOf(this.value);
        if(is_there){return false;};
        choosen.push(this.value);
        document.getElementsByName('taginput')[0].value += this.value + ' ';
    }
    

    Demo here

提交回复
热议问题