parseInt() 函数可解析一个字符串,并返回一个整数。

    1. <div id="app">
    2. <input type="text" v-model="n1">
    3. <select v-model="opt">
    4. <option value="+">+</option>
    5. <option value="-">-</option>
    6. <option value="*">*</option>
    7. <option value="/">/</option>
    8. </select>
    9. <input type="text" v-model="n2">
    10. <input type="button" value="=" @click="calc">
    11. <input type="text" v-model="result">
    12. </div>
    13. <script>
    14. var vm = new Vue({
    15. el: "#app",
    16. data: {
    17. n1: 0,
    18. n2: 0,
    19. result: 0,
    20. opt: "+"
    21. },
    22. methods: {
    23. calc() {
    24. switch (this.opt) {
    25. case '+':
    26. this.result = parseInt(this.n1) + parseInt(this.n2)
    27. break;
    28. case '-':
    29. this.result = parseInt(this.n1) - parseInt(this.n2)
    30. break;
    31. case '*':
    32. this.result = parseInt(this.n1) * parseInt(this.n2)
    33. break;
    34. case '/':
    35. this.result = parseInt(this.n1) / parseInt(this.n2)
    36. break;
    37. }
    38. }
    39. },
    40. })
    41. </script>