3
我正在使用vuejs和創建/刪除動態select
,它工作正常。如何獲取/設置vuejs中的select2值
這裏是工作提琴:https://jsfiddle.net/nikleshraut/fgpdp700/2/
var vm = new Vue({
el: "#app",
data: {
optionArr: [{id:1,price:100},{id:2,price:200}],
\t options: [{id:1,value:'option1'},{id:2,value:'option2'},{id:3,value:'option3'}]
},
mounted() {
\t console.log("help!!!!");
\t //$("#opt_select_0,#opt_select_1").select2();
},
methods: {
\t addOption: function(){
\t var index = Object.keys(this.optionArr).length;
\t this.optionArr.push({id:'',price:''});
setTimeout(function(){
//$("#opt_select_"+index).select2();
},100);
},
deleteOption: function(index){
\t this.optionArr.splice(index, 1);
},
getAll: function(){
\t console.log(this.optionArr);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css">
<div id="app">
<div>{{ $data.optionArr }}</div>
<template v-for="(user_option,index) in optionArr">
<select class="applySelect2" v-bind:id="'opt_select_'+index" on-change="print" v-model="user_option.id">
<template v-for="option in options">
<option v-bind:value="option.id">{{option.value}}</option>
</template>
</select> <input type="text" v-model="user_option.price"> <span style="cursor:pointer;color:red;" v-on:click="deleteOption(index)">delete</span><br/>
</template><br/>
<div><span style="cursor:pointer;" v-on:click="addOption">+add options</span></div>
<br/>
<div><span style="cursor:pointer;" v-on:click="getAll">Get All</span></div>
</div>
但是,如果我想用select2
,獲取或設置選擇2值無法按預期工作。同時刪除未working.here
https://jsfiddle.net/nikleshraut/fgpdp700/3/
哇的99%,除了刪除工作。如果我從第3行刪除第2個,它將刪除最後一個'select2',但可能是我可以自己修復此問題。非常感謝您的時間。 – C2486
歡迎!如果我的答案滿足您的問題要求,您可以接受我的答案!謝謝 ! – rahulsm