繁体   English   中英

选择(chosen-js):如何使用多个选择器取消选择

[英]Chosen (chosen-js): How to cancel selected with multiple selector

我正在Vue和Webpack上使用Chosen(chosen-js)库。 我想取消使用多个选择器的选择。 但是,我不能那样。 如何取消选择?

<script>
import $ from 'jquery';
window.$ = window.jQuery = $;
import chosen from 'chosen-js';
import 'chosen-js/chosen.css';

export default {
    mounted: function() {
        $('.select').chosen({
            width: '100%',
        }).change(function(ev, result) {
            if (result.selected === 'aaa') {
                // TODO: cancel selected
                console.log('selected', result.selected);
            }
        });
    },
};
</script>

<template>
    <select class="select" multiple>
        <option>aaa</option>
        <option>bbb</option>
        <option>ccc</option>
    </select>
</template>

它是通过以下代码实现的。

$('.select-company').chosen({
    width: width,
}).change(function(ev, result) {
    if (result.selected !== 'target value') {
        return;
    }

    let val = $('.my-selector').val();
    for (let i = 0; i < val.length; ++i) {
        if (val[i] === result.selected) {
            val.splice(i, 1);
            --i;
        }
    }

    $('.my-selector').val(val);
    $('.my-selector').trigger('chosen:updated');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM