如何根据html vue js中的select选项添加行?

时间:2018-03-28 15:48:28

标签: javascript jquery html vue.js vue-component

这是我的第一行。基于此处的选择选项,我需要选择不同的行

 <div class="row">
    <div class="col-md-4">
    <div class="form-group label-floating">
    <label class="control-label">Case Type</label>
    <select class="form-control" v-model="Type" id="type">
    <option value="1">One</option>
    <option value="2">Two</option>  
    <option value="3">Three</option>     
    </select>
    </div>
    </div>
    </div>

如果我选择选项1,我需要显示以下行

 <div class="row">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Date Released</label>
 <input type="date" class="form-control" v-model="released" required="">
 </div>
 </div>
 </div>

如果我选择选项2,我需要显示以下行

 <div class="row">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Full Name</label>
 <input type="date" class="form-control" v-model="fullname" required="">
 </div>
 </div>
 </div>

如果我选择选项3,我需要显示以下行

 <div class="row">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Address</label>
 <input type="date" class="form-control" v-model="address" required="">
 </div>
 </div>
 </div>

我怎样才能在html vue js中实现以下情况?根据选项选择,我需要显示上面的行。

我的vue js代码是

addForm = new Vue({
    el: "#addForm",
    data: {
        Type: '',
        released: '',
        fullname:'',
        address: '',
    },
    methods: {
        handleSubmit: function(e) {
            var vm = this;
            data['Type'] = this.Type;
            data['address'] = this.address;
            data['fullname'] = this.fullname;
            data['released'] = this.released;
            $.ajax({
                url: 'http://localhost:3000/f/',
                data: data,
                type: "POST",
                dataType: 'json',
                success: function(e) {
                    if (e.status) {
                        vm.response = e;
                        alert("success")
                    } else {
                        vm.response = e;
                        console.log(vm.response);
                        alert(" Failed") 
                    }
                }
            });
            return false;
        }, 
    },
});

1 个答案:

答案 0 :(得分:2)

使用Conditional Rendering

如下所示:

步骤1:将<select>绑定到* selectedType'

第2步:使用v-if确定应显示哪一个。

new Vue({
    el: "#addForm",
    data: {
        selectedType: '',
        address:'',
        fullname:'',
        released:''
    },
    methods: {
    }
});
<script src="https://unpkg.com/vue@2.0.1/dist/vue.js"></script>
<div id="addForm">
 <div class="row">
    <div class="col-md-4">
    <div class="form-group label-floating">
    <label class="control-label">Case Type</label>
    <select class="form-control" v-model="selectedType">
    <option value="1">One</option>
    <option value="2">Two</option>  
    <option value="3">Three</option>     
    </select>
    </div>
    </div>
  <div>

 <div class="row" v-if="selectedType==='1'">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Date Released</label>
 <input type="date" class="form-control" v-model="released" required="">
 </div>
 </div>
 </div>



 <div class="row" v-if="selectedType==='2'">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Full Name</label>
 <input type="date" class="form-control" v-model="fullname" required="">
 </div>
 </div>
 </div>



 <div class="row" v-if="selectedType==='3'">
 <div class="col-md-4">
 <div class="form-group label-floating">
 <label class="control-label">Address</label>
 <input type="date" class="form-control" v-model="address" required="">
 </div>
 </div>
 </div>
</div>