vue-form表单与table表格的数据关联

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        input{
            margin-left: 50px  ;
        }
        span{
            margin-left: 50px ;
        }
        select{
            margin-left: 50px ;
        }
        .create{
            margin-left: 150px ;
        }

    </style>
</head>
<body>
<form action="#"  id="app">
    <fieldset>
        <legend>Creat New Person</legend>
        <span>Name:</span><input type="text" v-model="text0">
        <br>
        <span>Age:</span><input type="text" value="0" v-model="text1">
        <br>
        <span>Sex:</span><select v-model="text2">
        <option>Man</option>
        <option>Woman</option>
        <option>....</option>
    </select>
        <br>
        <button class="create" @click="add">Create</button>
    </fieldset>
    <table>
        <tr><td>Name</td><td>Age</td><td>Sex</td><td>Delete</td></tr>
        <tr v-for="x in person"><td>{{x.name}}</td><td>{{x.age}}</td><td>{{x.sex}}</td><td><button @click="fun">Delete</button></td></tr>
    </table>
</form>
</body>
<script src="vue.js"></script>
<script>
    const app=new Vue({
        el:"#app",
        data:{
            text0:"",
            text1:"",
            text2:"",
            person:[{
                name:"Jack",
                age:"20",
                sex:"man",
            },
                {
                    name:"Bill",
                    age:"24",
                    sex:"woman",
                },
            ]
        },
        methods: {
            add(){
                    if (this.text0==""||this.text1==""){
                        alert("Name Or Age undefined")
                    }else{
                        this.person.push({
                            name: this.text0,
                            age: this.text1,
                            sex: this.text2,
                        });
                    }
            },
            fun(){
                this.person.pop()
            }
        }
    })
</script>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42413689/article/details/81710515
今日推荐