Skip to content

Commit 0674509

Browse files
committed
avoid triggering select reset when selectedIndex has not changed (fix #3504)
1 parent eef040e commit 0674509

File tree

1 file changed

+14
-7
lines changed
  • src/platforms/web/runtime/directives

1 file changed

+14
-7
lines changed

src/platforms/web/runtime/directives/model.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ export default {
6464
function setSelected (el, binding, vm) {
6565
const value = binding.value
6666
const isMultiple = el.multiple
67-
if (!isMultiple) {
68-
el.selectedIndex = -1
69-
} else if (!Array.isArray(value)) {
67+
if (isMultiple && !Array.isArray(value)) {
7068
process.env.NODE_ENV !== 'production' && warn(
7169
`<select multiple v-model="${binding.expression}"> ` +
7270
`expects an Array value for its binding, but got ${
@@ -76,17 +74,26 @@ function setSelected (el, binding, vm) {
7674
)
7775
return
7876
}
77+
let selected, option
7978
for (let i = 0, l = el.options.length; i < l; i++) {
80-
const option = el.options[i]
79+
option = el.options[i]
8180
if (isMultiple) {
82-
option.selected = value.indexOf(getValue(option)) > -1
81+
selected = value.indexOf(getValue(option)) > -1
82+
if (option.selected !== selected) {
83+
option.selected = selected
84+
}
8385
} else {
8486
if (getValue(option) === value) {
85-
el.selectedIndex = i
86-
break
87+
if (el.selectedIndex !== i) {
88+
el.selectedIndex = i
89+
}
90+
return
8791
}
8892
}
8993
}
94+
if (!isMultiple) {
95+
el.selectedIndex = -1
96+
}
9097
}
9198

9299
function hasNoMatchingOption (value, options) {

0 commit comments

Comments
 (0)