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

后端 未结 4 455
[愿得一人]
[愿得一人] 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:44

    You can see this fiddle :

    http://jsfiddle.net/ppSv4/

    $('select option').on('click',function(){
        $('#texthere').val( $(this).attr('value') );
    });
    
    0 讨论(0)
  • 2021-01-26 19:48

    You can do this, it finds the selected options, creates an array of text, then adds it to the text input.

    $("select[name=tags]").change(function() {
        var arrSelected = $(this).find("option:selected").map(function() {
            return $(this).text();
        }).get();
    
        $("input[name=taginput]").val(arrSelected);
    });
    

    Demo: http://jsfiddle.net/SyAN6/

    0 讨论(0)
  • 2021-01-26 19:51

    You can do this using jquery, simply by checking for a change in the multi select box and then adding the newly changed data to the input field.

    You can also the jsFiddle http://jsfiddle.net/eUDRV/85/

    $("#values").change(function () {
        var selectedItem = $("#values option:selected");
        $("#txtRight").val( $("#txtRight").val() +  selectedItem.text());
    });
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题