문제

Is it possible to preselect some options of a multiselectbox that won't be deselected by adding a new option select?

I now have a normal multiple selectbox, which options 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 at option A, option B and D will be deselected and A will be selected. I'd like to only deselect an item when someone clicks at an already selected option. When someone clicks at an option which is not yet selected, it has to be added to the already selected items.

What is the best way to do that? Or isn't that possible?

도움이 되었습니까?

해결책

That is easy with javascript (jQuery), like this:

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

See working demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top