根据select选项在html vue js中添加行?

时间:2018-04-24 10:43:15

标签: javascript 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="One">One</option>
    <option value="Two">Two</option>  
    <option value="Three">Three</option>     
    </select>
    </div>
    </div>
    </div>

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

 <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>

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

 <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>

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

 <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,2,3作为值。我需要一,二,三

请帮我解决问题

1 个答案:

答案 0 :(得分:0)

您可以在每个输入/标签上使用v-if

<div class="row">
  <div class="col-md-4">
    <div class="form-group label-floating">

     <label class="control-label" v-if="Type == 'One'">Date Released</label>
     <input type="date" class="form-control" v-if="Type == 'One'" v-model="released" required="">

     <label class="control-label" v-if="Type == 'Two'">Full Name</label>
     <input type="date" class="form-control" v-model="fullname" required=""  v-if="Type == 'Two'">

     <label class="control-label" v-if="Type == 'Three'">Address</label>
     <input type="date" class="form-control" v-model="address" required="" v-if="Type == 'Three'">
    </div>
   </div>
 </div>

或者对于标签,您可以拥有一个将类型映射到文本的对象

typeMapping: {
  'One': 'Date Released',
  'Two': 'Full Name'
  'Three': 'Address'
}

然后用一个标签

替换第一个代码示例中的标签
<label class="control-label">typeMapping[Type]</label>