diff --git a/src/platforms/web/compiler/directives/model.js b/src/platforms/web/compiler/directives/model.js index c5bcb52a..416091b1 100644 --- a/src/platforms/web/compiler/directives/model.js +++ b/src/platforms/web/compiler/directives/model.js @@ -163,13 +163,14 @@ function genSelect ( } const number = modifiers && modifiers.number - const assignment = `Array.prototype.filter` + + const selectedVal = `Array.prototype.filter` + `.call($event.target.options,function(o){return o.selected})` + `.map(function(o){var val = "_value" in o ? o._value : o.value;` + - `return ${number ? '_n(val)' : 'val'}})` + - (el.attrsMap.multiple == null ? '[0]' : '') + `return ${number ? '_n(val)' : 'val'}})` - const code = genAssignmentCode(value, assignment) + const assignment = '$event.target.multiple ? $$selectedVal : $$selectedVal[0]' + let code = `var $$selectedVal = ${selectedVal};` + code = `${code} ${genAssignmentCode(value, assignment)}` addHandler(el, 'change', code, null, true) } diff --git a/test/unit/features/directives/model-select.spec.js b/test/unit/features/directives/model-select.spec.js index 98288b13..31b9350d 100644 --- a/test/unit/features/directives/model-select.spec.js +++ b/test/unit/features/directives/model-select.spec.js @@ -262,6 +262,31 @@ describe('Directive v-model select', () => { }) } + it('should work with multiple binding', (done) => { + const spy = jasmine.createSpy() + const vm = new Vue({ + data: { + isMultiple: true, + selections: ['1'] + }, + template: + '', + watch: { + selections: spy + } + }).$mount() + document.body.appendChild(vm.$el) + vm.$el.options[1].selected = true + triggerEvent(vm.$el, 'change') + waitForUpdate(() => { + expect(spy).toHaveBeenCalled() + expect(vm.selections).toEqual(['1', '2']) + }).then(done) + }) + it('multiple with static template', () => { const vm = new Vue({ template: