Skip to content

Commit

Permalink
fix(v-model) selected option not updated properly under IE11 (#6213)
Browse files Browse the repository at this point in the history
fix #6209
  • Loading branch information
jkzing authored and yyx990803 committed Aug 29, 2017
1 parent 5091e2c commit f40da5d
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions src/platforms/web/runtime/directives/model.js
Expand Up @@ -22,14 +22,7 @@ if (isIE9) {
export default {
inserted (el, binding, vnode) {
if (vnode.tag === 'select') {
const cb = () => {
setSelected(el, binding, vnode.context)
}
cb()
/* istanbul ignore if */
if (isIE || isEdge) {
setTimeout(cb, 0)
}
setSelected(el, binding, vnode.context)
el._vOptions = [].map.call(el.options, getValue)
} else if (vnode.tag === 'textarea' || isTextInputType(el.type)) {
el._vModifiers = binding.modifiers
Expand Down Expand Up @@ -67,37 +60,45 @@ export default {
}

function setSelected (el, binding, vm) {
const value = binding.value
const isMultiple = el.multiple
if (isMultiple && !Array.isArray(value)) {
process.env.NODE_ENV !== 'production' && warn(
`<select multiple v-model="${binding.expression}"> ` +
`expects an Array value for its binding, but got ${
Object.prototype.toString.call(value).slice(8, -1)
}`,
vm
)
return
}
let selected, option
for (let i = 0, l = el.options.length; i < l; i++) {
option = el.options[i]
if (isMultiple) {
selected = looseIndexOf(value, getValue(option)) > -1
if (option.selected !== selected) {
option.selected = selected
}
} else {
if (looseEqual(getValue(option), value)) {
if (el.selectedIndex !== i) {
el.selectedIndex = i
const cb = () => {
const value = binding.value
const isMultiple = el.multiple
if (isMultiple && !Array.isArray(value)) {
process.env.NODE_ENV !== 'production' && warn(
`<select multiple v-model="${binding.expression}"> ` +
`expects an Array value for its binding, but got ${
Object.prototype.toString.call(value).slice(8, -1)
}`,
vm
)
return
}
let selected, option
for (let i = 0, l = el.options.length; i < l; i++) {
option = el.options[i]
if (isMultiple) {
selected = looseIndexOf(value, getValue(option)) > -1
if (option.selected !== selected) {
option.selected = selected
}
} else {
if (looseEqual(getValue(option), value)) {
if (el.selectedIndex !== i) {
el.selectedIndex = i
}
return
}
return
}
}
if (!isMultiple) {
el.selectedIndex = -1
}
}
if (!isMultiple) {
el.selectedIndex = -1

cb()
/* istanbul ignore if */
if (isIE || isEdge) {
setTimeout(cb, 0)
}
}

Expand Down

0 comments on commit f40da5d

Please sign in to comment.