Skip to content

Commit 318e77b

Browse files
committed
Merge pull request vuejs#1299 from evantre/dev
fix select element can't removeChild optgroup's option
2 parents fe9a3db + 1774e99 commit 318e77b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/directives/model/select.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ function initOptions (expression) {
9898
while (i--) {
9999
var option = el.options[i]
100100
if (option !== defaultOption) {
101-
el.removeChild(option)
101+
var parentNode = option.parentNode
102+
if (parentNode === el) {
103+
parentNode.removeChild(option)
104+
} else {
105+
el.removeChild(parentNode)
106+
}
102107
}
103108
}
104109
buildOptions(el, value)

test/unit/specs/directives/model_spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ if (_.inBrowser) {
341341
expect(opts[2].selected).toBe(true)
342342
})
343343

344-
it('select + options + optgroup', function () {
345-
new Vue({
344+
it('select + options + optgroup', function (done) {
345+
var vm = new Vue({
346346
el: el,
347347
data: {
348348
test: 'b',
@@ -365,6 +365,26 @@ if (_.inBrowser) {
365365
expect(opts[0].selected).toBe(false)
366366
expect(opts[1].selected).toBe(true)
367367
expect(opts[2].selected).toBe(false)
368+
vm.opts = [
369+
{ label: 'X', options: ['x', 'y'] },
370+
{ label: 'Y', options: ['z'] }
371+
]
372+
vm.test = 'y'
373+
_.nextTick(function () {
374+
expect(el.firstChild.innerHTML).toBe(
375+
'<optgroup label="X">' +
376+
'<option value="x">x</option><option value="y">y</option>' +
377+
'</optgroup>' +
378+
'<optgroup label="Y">' +
379+
'<option value="z">z</option>' +
380+
'</optgroup>'
381+
)
382+
var opts = el.firstChild.options
383+
expect(opts[0].selected).toBe(false)
384+
expect(opts[1].selected).toBe(true)
385+
expect(opts[2].selected).toBe(false)
386+
done()
387+
})
368388
})
369389

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

0 commit comments

Comments
 (0)