Skip to content

Commit dbaabb2

Browse files
evantreyyx990803
authored andcommitted
fix select element can't removeChild optgroup's option
1 parent 0d171b0 commit dbaabb2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/directives/model/select.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ function initOptions (expression) {
101101
while (i--) {
102102
var option = el.options[i]
103103
if (option !== defaultOption) {
104-
el.removeChild(option)
104+
var parentNode = option.parentNode
105+
if (parentNode === el) {
106+
parentNode.removeChild(option)
107+
} else {
108+
el.removeChild(parentNode)
109+
}
105110
}
106111
}
107112
buildOptions(el, value)

test/unit/specs/directives/model_spec.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ if (_.inBrowser) {
395395
expect(opts[2].selected).toBe(true)
396396
})
397397

398-
it('select + options + optgroup', function () {
399-
new Vue({
398+
it('select + options + optgroup', function (done) {
399+
var vm = new Vue({
400400
el: el,
401401
data: {
402402
test: 'b',
@@ -419,6 +419,26 @@ if (_.inBrowser) {
419419
expect(opts[0].selected).toBe(false)
420420
expect(opts[1].selected).toBe(true)
421421
expect(opts[2].selected).toBe(false)
422+
vm.opts = [
423+
{ label: 'X', options: ['x', 'y'] },
424+
{ label: 'Y', options: ['z'] }
425+
]
426+
vm.test = 'y'
427+
_.nextTick(function () {
428+
expect(el.firstChild.innerHTML).toBe(
429+
'<optgroup label="X">' +
430+
'<option value="x">x</option><option value="y">y</option>' +
431+
'</optgroup>' +
432+
'<optgroup label="Y">' +
433+
'<option value="z">z</option>' +
434+
'</optgroup>'
435+
)
436+
var opts = el.firstChild.options
437+
expect(opts[0].selected).toBe(false)
438+
expect(opts[1].selected).toBe(true)
439+
expect(opts[2].selected).toBe(false)
440+
done()
441+
})
422442
})
423443

424444
it('select + options with Object value', function (done) {

0 commit comments

Comments
 (0)