[vue basics] 10 basic use of v-model

Table of contents

1. Use in single-line/multi-line text

Second, use in the check box

3. Use in the radio button

4. Use in the drop-down box

5. Modifiers


1. Use in single-line/multi-line text

<!DOCTYPE html>
<html lang="en">

<head>
    <script type="text/javascript" src="../js/vue.js"></script>
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title></title>
</head>

<body>
    <div id="root">
        <div>
            <label>用户名:</label><input type="text" v-model="user.username">
        </div>
        <div>
            <label>简介:</label><textarea v-model="user.introduction"></textarea>
        </div>

        {
   
   {user.username}}
        {
   
   {user.introduction}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                user: {
                    username: "admin",
                    introduction: "我是admin..."
                }
            },

        })
    </script>
</body>

</html>

Second, use in the check box

<!DOCTYPE html>
<html lang="en">

<head>
    <script type="text/javascript" src="../js/vue.js"></script>
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title></title>
</head>

<body>
    <div id="root">
        <div>
            <input type="checkbox" v-model="user.hobby" value="1"><label>Chinese</label>
            <input type="checkbox" v-model="user.hobby" value="2"><label>Math</label>
            <input type="checkbox" v-model="user.hobby" value="3"><label>English</label>
        </div>
        {
   
   {user.hobby}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                user: {
                    hobby: []
                }
            },

        })
    </script>
</body>

</html>

3. Use in the radio button

<div id="root">
        <div>
            <input type="radio" v-model="user.sex" value="1"><label>男</label>
            <input type="radio" v-model="user.sex" value="2"><label>女</label>
        </div>
        {
   
   {user.sex}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                user: {
                    sex: "1"
                }
            },

        })
    </script>

4. Use in the drop-down box

<div id="root">
        <div>
            <select v-model="user.cityIndex">
                <option value="1">北京</option>
                <option value="2">上海</option>
                <option value="3">深圳</option>
            </select>
        </div>
        {
   
   {user.cityIndex}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                user: {
                    cityIndex: "1"
                }
            },
        })
    </script>

Combined with v-for

<body>
    <div id="root">
        <div>
            <select v-model="cityIndexChooesd">
                <option v-for="(city, index) in cityList" :value="city.value">{
   
   {city.name}}</option>
            </select>
        </div>
        {
   
   {cityIndexChooesd}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                cityIndexChooesd: "01",
                cityList: [{
                        name: "北京",
                        value: "01"
                    },
                    {
                        name: "上海",
                        value: "02"
                    },
                    {
                        name: "深圳",
                        value: "03"
                    },
                ]
            },

        })
    </script>
</body>

 5. Modifiers

<body>
    <div id="root">
        <div>
            <input type="text" v-model.lazy="content">
        </div>
        {
   
   {content}}
    </div>

    <script>
        new Vue({
            el: '#root',
            data: {
                content: ""
            },

        })
    </script>
</body>

Guess you like

Origin blog.csdn.net/ChaoChao66666/article/details/130718824