如何解决select2搜索字段和Bootstrap模式之间的冲突?

时间:2017-05-16 08:32:33

标签: twitter-bootstrap bootstrap-modal jquery-select2

我与select2和boostrap模式发生冲突。我不能再使用搜索字段了:

    public class User implements Parcelable{

        String name;


        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        /**
        * Storing the User data to Parcel object
        **/
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);


        }

        /**
        * A constructor that initializes the User object
        **/
        public User(String uName){
            this.name = uName;

        }

        /**
        * Retrieving User data from Parcel object
        * This constructor is invoked by the method createFromParcel(Parcel source) of
        * the object CREATOR
        **/
        private Student(Parcel in){
            this.mSName = in.readString();

        }

        public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {

            @Override
            public User createFromParcel(Parcel source) {
                return new User(source);
            }

            @Override
            public User[] newArray(int size) {
                return new User[size];
            }
        };
    }

public class MainActivity extends Activity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

       mBtnOk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // Creating an instance of Student class with user input data
                    User userObj= new User ("Name");

                    // Creating an intent to open the activity 
                    Intent intent = new Intent(MainActivity.this, NextActivity.class);

                    // Passing data as a parecelable object to 
                    intent.putExtra("user",userObj);

                    // Opening the activity
                    startActivity(intent);
                }
            });



    }

    }





    public class NextActivity extends Activity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next);

     // Fetching data from a parcelable object passed from MainActivity
            User user= getIntent().getParcelableExtra("user");



    }

    }
$(document).ready(function() {

$('.select2').select2();

  $('#myModal').on('show.bs.modal', function() {
    $('.select2').select2();
  })
  
  $('#myModal').on('hidden.bs.modal', function() {
    $('.select2').select2('destroy');
  })
});

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的有效方法:

$(document).ready(function() {

  $('.select2').select2();

  $('#myModal').on('show.bs.modal', function() {
     $('.select2').select2({
        dropdownParent: $('#myModal')
     });
  })

  $('#myModal').on('hidden.bs.modal', function() {
    $('.select2').select2('destroy');
  })
});