Changing selection in a select with the Chosen plugin

后端 未结 4 1813
时光说笑
时光说笑 2020-12-08 01:42

I\'m trying to change the currently selected option in a select with the Chosen plugin.

The documentation covers updating the list, and triggering an event when an o

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 02:35

    In case of multiple type of select and/or if you want to remove already selected items one by one, directly within a dropdown list items, you can use something like:

    jQuery("body").on("click", ".result-selected", function() {
        var locID = jQuery(this).attr('class').split('__').pop();
        // I have a class name: class="result-selected locvalue__209"
        var arrayCurrent = jQuery('#searchlocation').val();
        var index = arrayCurrent.indexOf(locID);
        if (index > -1) {
            arrayCurrent.splice(index, 1);
        }
        jQuery('#searchlocation').val(arrayCurrent).trigger('chosen:updated');
    });
    

提交回复
热议问题