feat(jqLite): return [] for .val() on <select multiple> with no selection
Fixes #14370 BREAKING CHANGE: For the jqLite element representing a select element in the multiple variant with no options chosen the .val() getter used to return null and now returns an empty array. To migrate the code follow the example below: Before: HTML: <select multiple> <option>value 1</option> <option>value 2</option> </select> JavaScript: var value = $element.val(); if (value) { /* do something */ } After: HTML: <select multiple> <option>value 1</option> <option>value 2</option> </select> JavaScript: var value = $element.val(); if (value.length > 0) { /* do something */ }
This commit is contained in:
+1
-1
@@ -699,7 +699,7 @@ forEach({
|
||||
result.push(option.value || option.text);
|
||||
}
|
||||
});
|
||||
return result.length === 0 ? null : result;
|
||||
return result;
|
||||
}
|
||||
return element.value;
|
||||
}
|
||||
|
||||
@@ -1034,6 +1034,31 @@ describe('jqLite', function() {
|
||||
'<option>test 2</option>' +
|
||||
'</select>').val()).toEqualOneOf(null, []);
|
||||
});
|
||||
|
||||
it('should get an empty array from a multi select if no elements are chosen', function() {
|
||||
// In jQuery < 3.0 .val() on select[multiple] with no selected options returns an
|
||||
// null instead of an empty array.
|
||||
// See https://github.com/jquery/jquery/issues/2562 for more details.
|
||||
if (isJQuery2x()) return;
|
||||
|
||||
expect(jqLite(
|
||||
'<select multiple>' +
|
||||
'<optgroup>' +
|
||||
'<option>test 1</option>' +
|
||||
'<option>test 2</option>' +
|
||||
'</optgroup>' +
|
||||
'<option>test 3</option>' +
|
||||
'</select>').val()).toEqual([]);
|
||||
|
||||
expect(jqLite(
|
||||
'<select multiple>' +
|
||||
'<optgroup disabled>' +
|
||||
'<option>test 1</option>' +
|
||||
'<option>test 2</option>' +
|
||||
'</optgroup>' +
|
||||
'<option disabled>test 3</option>' +
|
||||
'</select>').val()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user