computed-repeat.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Repeated form elements</title>
  5. <meta charset="utf-8">
  6. <script src="../../../dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <form id="form">
  10. <p v-repeat="items">
  11. <input type="text" name="text{{$index}}" v-model="text">
  12. </p>
  13. <button v-on="click: add" id="add">Add</button>
  14. <p id="texts">{{texts}}</p>
  15. </form>
  16. <script>
  17. var app = new Vue({
  18. el: '#form',
  19. data: {
  20. items: [
  21. { text: "a" },
  22. { text: "b" }
  23. ]
  24. },
  25. methods: {
  26. add: function(e) {
  27. this.items.push({ text: "c" })
  28. e.preventDefault()
  29. }
  30. },
  31. computed: {
  32. texts: function () {
  33. return this.items.map(function(item) {
  34. return item.text
  35. }).join(",")
  36. }
  37. }
  38. })
  39. </script>
  40. </body>
  41. </html>