• v-model绑定input、textarea、checkbox、radio、select


    1.input

    1. <div>
    2. <!-- v-model绑定input -->
    3. <input type="text" v-model="message">
    4. <h2>{{message}}</h2>
    5. </div>
    6. <script>
    7. const App={
    8. template:'#my-app',
    9. data() {
    10. return {
    11. message:'Hello World',
    12. }
    13. },
    14. }
    15. Vue.createApp(App).mount('#app')
    16. </script>

    效果如下:

    2.textarea

    1. <div>
    2. <!-- v-model绑定textarea -->
    3. <textarea name="" id="" cols="30" rows="3" v-model="texta"></textarea>
    4. <h2>{{texta}}</h2>
    5. </div>
    6. <script>
    7. const App={
    8. template:'#my-app',
    9. data() {
    10. return {
    11. texta:"textarea",
    12. }
    13. },
    14. }
    15. Vue.createApp(App).mount('#app')
    16. </script>

    效果如下:

    3.checkbox

    1. <div>
    2. <!-- v-model绑定多选checkbox【多选框绑定v-model显示必须写value,v-model显示的值就是value中的值】-->
    3. <span>你的爱好:</span>
    4. <label for="basketball">
    5. <input type="checkbox" v-model="hobbies" value="basketball">篮球
    6. </label>
    7. <label for="football">
    8. <input type="checkbox" v-model="hobbies" value="football">足球
    9. </label>
    10. <label for="badminton">
    11. <input type="checkbox" v-model="hobbies" value="badminton">羽毛球
    12. </label>
    13. <label for="tennis">
    14. <input type="checkbox" v-model="hobbies" value="tennis">网球
    15. </label>
    16. <h2>{{hobbies}}</h2>
    17. </div>
    18. <script>
    19. const App={
    20. template:'#my-app',
    21. data() {
    22. return {
    23. hobbies:[],
    24. }
    25. },
    26. }
    27. Vue.createApp(App).mount('#app')
    28. </script>

    效果如下:

    4.radio

    1. <div>
    2. <!-- v-model绑定单选radio -->
    3. <span>性别:</span>
    4. <label for="male">
    5. <input type="radio" v-model="gender" value="male">
    6. </label>
    7. <label for="female">
    8. <input type="radio" v-model="gender" value="female">
    9. </label>
    10. <h2>性别选项是{{gender}}</h2>
    11. </div>
    12. <script>
    13. const App={
    14. template:'#my-app',
    15. data() {
    16. return {
    17. gender:'',
    18. }
    19. },
    20. }
    21. Vue.createApp(App).mount('#app')
    22. </script>

    效果如下:

    5.select

    1. <div>
    2. <!-- v-model绑定select下拉框(select添加multiple size="2" 可多选至2) -->
    3. <select v-model="fruit" name="" id="">
    4. <option value="apple">苹果</option>
    5. <option value="banana">香蕉</option>
    6. <option value="orange">橙子</option>
    7. </select>
    8. <h2>水果{{fruit}}</h2>
    9. </div>
    10. <script>
    11. const App={
    12. template:'#my-app',
    13. data() {
    14. return {
    15. fruit:[],
    16. }
    17. },
    18. }
    19. Vue.createApp(App).mount('#app')
    20. </script>

    效果如下:

  • 相关阅读:
    ubuntu忘记mysql密码,怎么办
    java私钥加密——SHA256withRSA
    axios的post请求所有传参方式
    23种设计模式之迭代器模式(Iterator Pattern)
    数据预处理和特征工程1--无量纲化:数据归一化、标准化
    TIL: 申请一个30天的体验版本的 Salesforce Consumer Goods Cloud Org
    【案例分享】华为防火墙出接口方式的单服务器智能DNS配置
    ES6新增关键字:let和const及let的常用场景
    最近公共祖先(lca)
    亚马逊国际获得AMAZON商品详情 API
  • 原文地址:https://blog.csdn.net/weixin_45096939/article/details/133776324