Chosen harvesthq resize width dynamically

前端 未结 12 1645
渐次进展
渐次进展 2020-12-30 03:49

How can you have a harvesthq Chosen dropdown with a dynamic width style?

By default it has a fixed width and if you try to modify it with CSS you will have several p

12条回答
  •  忘掉有多难
    2020-12-30 04:06

    I had a responsive form, which had lot of CSS rules with media queries, !important, etc and was using chosen with class inheritance option. On resizing, there were often conflicts between chosen and parent css and things would break. I came up with a simple solution that worked for me: recreate chosen dom on every window resize:

    jQuery(document).ready(function() {
            doResize();
            jQuery(window).on('resize', doResize);
    });
    
    
    function doResize() {
         // Parent website's code, that would also reorganize select elements on the page
        jQuery("select.my-select").removeAttr("style"); // Remove display:none style added by chosen
        jQuery("select.my-select").removeClass("chzn-done"); // Remove chzn-done class added by chosen
    
        jQuery(".chzn-container").remove();
        jQuery("select.my-select").chosen({
                     width: '100%',
                     disable_search_threshold: 10,
                     inherit_select_classes : true
                   });
    }
    

提交回复
热议问题