MDL-51324 forms: fix autocomplete element double selection bug

Incorrect types were preventing the currently selected values from
being removed from the new values returned via ajax.

Also fix a selection bug with autocomplete+ajax+singleselect.
This commit is contained in:
Damyon Wiese
2016-03-09 13:30:27 +08:00
parent 235ef57a3d
commit 91ab264cf5
2 changed files with 11 additions and 3 deletions
File diff suppressed because one or more lines are too long
+10 -2
View File
@@ -469,12 +469,20 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
if (!option.prop('selected')) {
option.remove();
} else {
existingValues.push(option.attr('value'));
existingValues.push(String(option.attr('value')));
}
});
if (!options.multiple && originalSelect.children('option').length === 0) {
// If this is a single select - and there are no current options
// the first option added will be selected by the browser. This causes a bug!
// We need to insert an empty option so that none of the real options are selected.
var option = $('<option>');
originalSelect.append(option);
}
// And add all the new ones returned from ajax.
$.each(processedResults, function(resultIndex, result) {
if (existingValues.indexOf(result.value) === -1) {
if (existingValues.indexOf(String(result.value)) === -1) {
var option = $('<option>');
option.append(result.label);
option.attr('value', result.value);