multiple select element - onchange

后端 未结 9 1991
栀梦
栀梦 2021-02-18 22:50

I have a select element that allows for multiple selections. I\'d like to display the selected values in another part of the page (in a div or something) as the user makes chang

9条回答
  •  名媛妹妹
    2021-02-18 23:22

    I'm doing a form submit. My template helper looks like this:

     'submit #update': function(event) {
        event.preventDefault();
        var obj_opts = event.target.tags.selectedOptions; //returns HTMLCollection
        var array_opts = Object.values(obj_opts);  //convert to array
        var stray = array_opts.map((o)=> o.text ); //to filter by: text, value or selected
        //stray is now ["Test", "Milk Free"] for example, depending on the selection
        //...do stuff...
    }
    

    You could use a similar pattern for 'onchange'

提交回复
热议问题