How to pre-select multiple selection options? (html + php)

Is it possible to pre-select some multiselex parameters that will not be canceled when a new option is added?

Now I have the usual multiple selectbox, which parameters will be preselected by PHP:

<select name="selectbox" multiple> <option value="A">A</option> <option value="B" selected>B</option> <option value="C">C</option> <option value="D" selected>D</option> </select> 

When I click on option A, options B and D will be canceled and A. will be selected. I would only like to deselect an item when someone clicks on an option already selected. When someone clicks on a parameter that is not already selected, it must be added to the already selected elements.

What is the best way to do this? Or is it not possible?

+4
source share
1 answer

This is easy with javascript (jQuery), for example:

 $('select[name=selectbox]').on('mousedown','option',function(ev) { ev.preventDefault(); $(this).prop('selected', !$(this).is(':selected') ); }); 

See working demo

+1
source

Source: https://habr.com/ru/post/1479689/


All Articles