欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue.js監(jiān)聽(tīng)select2的值改變進(jìn)行查詢方式

 更新時(shí)間:2022年04月07日 12:49:22   作者:A吳廣智  
這篇文章主要介紹了Vue.js監(jiān)聽(tīng)select2的值改變進(jìn)行查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

監(jiān)聽(tīng)select2的值改變進(jìn)行查詢

由于前端項(xiàng)目使用的是Vue.js和bootstrap整合開(kāi)發(fā),中間用到了select2下拉框,今天在做查詢的時(shí)候,想根據(jù)下拉框的值變動(dòng)進(jìn)行監(jiān)聽(tīng)查詢,方法如下:

頁(yè)面中引用select2組件

<div class="input-group input-group-sm mb-3">
?? ?<select v-select2="" v-model="ruleAndRemindType" v-on:change="getChange(ruleAndRemindType)" data-placeholder="請(qǐng)選擇分類" ?class="js-example-placeholder-multiple col-sm-12">
?? ??? ?<option value="rule">規(guī)則設(shè)置</option>
?? ??? ?<option value="remind">提醒設(shè)置</option>
?? ?</select>
</div>

在js里引入如下代碼:

Vue.directive('select2', {
?? ?inserted: function (el, binding, vnode) {
?? ??? ?let options = binding.value || {};
?? ??? ?$(el).select2(options).on("select2:select", (e) => {
?? ??? ??? ?el.dispatchEvent(new Event('change', {target: e.target})); //說(shuō)好的雙向綁定,竟然不安套路
?? ??? ?});
?? ?},
?? ?update: function (el, binding, vnode) {
?? ??? ?for (var i = 0; i < vnode.data.directives.length; i++) {
?? ??? ??? ?if (vnode.data.directives[i].name == "model") {
?? ??? ??? ??? ?$(el).val(vnode.data.directives[i].value);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?$(el).trigger("change");
?? ?}
});

在vue實(shí)例中使用,進(jìn)行測(cè)試

var vm = new Vue({
?? ?el: '#app',
?? ?data:{
?? ??? ?ruleAndRemindType: 'rule'
?? ?},
?? ?methods: {
?? ??? ?//初始執(zhí)行
?? ??? ?init() {
?? ??? ??? ?this.getList('rule');
?? ??? ?},
?? ??? ?getChange: function (ruleAndRemindType) {
?? ??? ??? ?this.getList(ruleAndRemindType);
?? ??? ?},
?? ??? ?getList: function(ruleAndRemindType) {
?? ??? ??? ?alert(ruleAndRemindType);
?? ??? ?},
?? ?},?? ?
?? ?mounted(){
?? ??? ?setTimeout(function(){
?? ??? ??? ?vm.init();
?? ??? ?},50)
?? ?}
})

因?yàn)橛玫奖O(jiān)聽(tīng)值的變化進(jìn)行動(dòng)態(tài)查詢,所以查詢資料找到此辦法,親測(cè)可行 

監(jiān)聽(tīng)select的事件

<select @change="findItemNameBYClass">
? ? ? ?<option v-for="(name,index) in findItemName" :key="index">{{name}}</option>
? ? </select>

vue代碼

var vm = new Vue({
?? ?el : '#container',
?? ?data : {
?? ?},
?? ?methods:{
?? ??? ?findItemNameBYClass:function(e){
?? ??? ??? ??? ?console.log( e.target.value)
?? ??? ?}
?? ?}
})

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評(píng)論