chosen with bootstrap 3 not working properly

白昼怎懂夜的黑 提交于 2019-11-28 04:55:53

Applying Chosen after the modal is shown should solve your problem:

$('#myModal').on('shown.bs.modal', function () {
  $('.chosen-select', this).chosen();
});

Or when Chosen was already applied, destroy and reapply:

$('#myModal').on('shown.bs.modal', function () {
  $('.chosen-select', this).chosen('destroy').chosen();
});

Fiddle here: http://jsfiddle.net/koenpunt/W6dZV/

So in your case it would probably something like:

$('.someClick').click(function(e){
   e.preventDefault();
   x.modal('show');
   x.on('shown.bs.modal', function(){
       x.find('.modal-body').load('path/page.html', function(response, status, xhr){
           if(status == "success"){
               $("select[name=elementName]").chosen();
           }
       });
   });
});

EDIT

To complement Chosen you can use the Chosen Bootstrap theme

Try this, I digged in chosen and found out that just need to pass options with "width" property like in this, or any other width value:

$("select").chosen({width: "inherit"}) 

As described in Allow Chosen to be used in Bootstrap modal, the problem is within the chosen.jquery.js file. I found it on line 435 and just added the following code inside the else statement. check it out, it worked for me.

// Fixing problem when the select is not displayed.
   if ( this.form_field.offsetWidth == 0 ) {
      return "" + $(this.form_field).width() + "px";
    }

Adding width to your chosen class will solve this problem.

$('.someClick').click(function(e){
   e.preventDefault();
   x.modal('show');
   x.on('shown.bs.modal', function(){
       x.find('.modal-body').load('path/page.html', function(response, status, xhr){
           if(status == "success"){
               $("select[name=elementName]").chosen({ width:'100%' }); /* add width here !!! */
           }
       });
   });
});

I had the same problem today but I needed a quickly solution... but firs a screenshot of my problem:

This is my problem

so I did this:

1) The problem is the "width", so I saw on console this

take a look on the console, over the "select"

2) I made a quickly solution adding a new "width". As you can see in the previous image, there is a class 'class="chosen-container chosen-container-single', so I took the class "chosen-container" to add the new "width" when I call my "Modal". Here my code:

just add one line

so, basically I added this code:

$('.chosen-container').css({ 'width':'350px' });

to the function that call the modal. Feel free to copy and paste this on your function :) Maybe it is not a perfect solution but it works for me and maybe for you too.

sorry for my english, but I'm learnig. I speak spanish better than english. Greetings

PD: There is my result

The following solution worked for me (from http://codepen.io/m-e-conroy/pen/ALsdF):

Redefine "modal" class:

.modal { 
    display: block;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!