Closed cookieofcode closed 4 years ago
if (!val || val === ''){
empty_option.prop('selected', true);
options.push(empty_option);
$selectField.html(options);
//$selectField.trigger('change'); // don't trigger change if empty > unfortunately field stays
// blank instead of "--------"
return;
}
$.getJSON(url, function(j){
var prev_value = $(elem_id).children("option[selected='selected']").val(); // get prev_value
auto_choose = j.length == 1 && auto_choose;
// Append empty label as the first option
if (!(init_value || auto_choose)) {
empty_option.prop('selected', true);
}
options.push(empty_option);
// Append each option to the select
$.each(j, function (index, optionData) {
var option = $('<option></option>')
.prop('value', optionData.value)
.text(optionData.display);
if (auto_choose || init_value && optionData.value == init_value) {
option.prop('selected', true);
}
options.push(option);
});
$selectField.html(options);
var width = $selectField.outerWidth();
if (navigator.appVersion.indexOf("MSIE") != -1)
$selectField.width(width + 'px');
// check if changed and trigger then
if (parseInt(init_value) !== parseInt(prev_value))
$selectField.trigger('change');
});
@cookieofcode is this still happening with the latest master version?
@manelclos Thanks for asking. I can't check as I have no longer access to the project.
Checklist
Put an
x
in the bracket when you have completed each task, like this:[x]
master
branch of django-smart-selects.smart_selects
app.models.py
,forms.py
, andviews.py
with problems.Steps to reproduce
Actual behavior
When leaving the page without changing anything, a message "You have unsaved changes" appears. This is due to the "change listener" in the django-admin interface: `onFormInputChanged: function($inputs) { $inputs.off('change', this.onFormInputChanged);
To disable this behaviour you can uncomment
$selectField.trigger('change');
but this leads to unexpected behaviour.The bug is related to #91
Expected behavior
Don't change the content of the field (via 'change'), when nothing is selected but still work as expected.
Has anyone a suggestion how to solve this problem?