kflorence/jquery-deserialize

jquery-deserialize does not remove selection before deserialization multiple select

dshiryaev-plesk opened this issue · 8 comments

Version

1.3.5

Steps to reproduce

  • Make html form like the following:
<form>
	<select name="selectMultiple" multiple="multiple">
		<option value="1" selected="selected">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
	</select>
</form>
  • Run javascript code like the following:
    $("form").deserialize({selectMultiple:[2,3]});

Expected result

Select has selected options with values "2" and "3"

Actual result

Select has selected options with values "1", "2" and "3"

@dshiryaev-plesk hmm, why hardcode selected="selected" if you don't want it?

But it does seem like a bug, so I will look at it soon.

It's just for the example. If you delete selected="selected", then select the options manually, then run javascript code, you will get the same result.

@dshiryaev-plesk hmm, why hardcode selected="selected" if you don't want it?

I think there is no need to update the selected attribute programmatically as the browser will automatically know which element is selected. The selected attribute is intended to set a default option on page load.

From: https://www.w3schools.com/tags/att_option_selected.asp

"When present, it specifies that an option should be pre-selected when the page loads."

A pre-selected option seems "selected", but the $('#selectid option:selected').val() command just returns the new value set by the deserialization function.

@EnricoU i think the use case here is that calling deserialize on the form will potentially not properly set multiple select list values if there are existing values on the element -- basically after deserialization the form values would be in an invalid state because they don't match what was serialized.

Closing, as this should have been addressed with #39.