Просмотр исходного кода

v-attr should also set property for checked and selected

Evan You 10 лет назад
Родитель
Сommit
59c9456ea9
2 измененных файлов с 13 добавлено и 2 удалено
  1. 7 2
      src/directives/attr.js
  2. 6 0
      test/unit/specs/directives/attr_spec.js

+ 7 - 2
src/directives/attr.js

@@ -1,6 +1,11 @@
 // xlink
 // xlink
 var xlinkNS = 'http://www.w3.org/1999/xlink'
 var xlinkNS = 'http://www.w3.org/1999/xlink'
 var xlinkRE = /^xlink:/
 var xlinkRE = /^xlink:/
+var inputProps = {
+  value: 1,
+  checked: 1,
+  selected: 1
+}
 
 
 module.exports = {
 module.exports = {
 
 
@@ -35,12 +40,12 @@ module.exports = {
   },
   },
 
 
   setAttr: function (attr, value) {
   setAttr: function (attr, value) {
-    if (attr === 'value' && attr in this.el) {
+    if (inputProps[attr] && attr in this.el) {
       if (!this.valueRemoved) {
       if (!this.valueRemoved) {
         this.el.removeAttribute(attr)
         this.el.removeAttribute(attr)
         this.valueRemoved = true
         this.valueRemoved = true
       }
       }
-      this.el.value = value
+      this.el[attr] = value
     } else if (value != null && value !== false) {
     } else if (value != null && value !== false) {
       if (xlinkRE.test(attr)) {
       if (xlinkRE.test(attr)) {
         this.el.setAttributeNS(xlinkNS, attr, value)
         this.el.setAttributeNS(xlinkNS, attr, value)

+ 6 - 0
test/unit/specs/directives/attr_spec.js

@@ -32,6 +32,12 @@ if (_.inBrowser) {
       dir.update('what')
       dir.update('what')
       expect(dir.el.hasAttribute('value')).toBe(false)
       expect(dir.el.hasAttribute('value')).toBe(false)
       expect(dir.el.value).toBe('what')
       expect(dir.el.value).toBe('what')
+      dir.el = document.createElement('input')
+      dir.el.type = 'checkbox'
+      dir.arg = 'checked'
+      expect(dir.el.checked).toBe(false)
+      dir.update(true)
+      expect(dir.el.checked).toBe(true)
     })
     })
 
 
     it('xlink', function () {
     it('xlink', function () {