How can I make select2 read only and not disabled?

68 Views Asked by At

I write a view page with asp core and use select2 for my combo box .when use disable this line code work good:

$('select').prop("disabled", true);

but it disable and not readonly. i want readonly. in readonly value save but in disable valu not save and posted null value to controller. in select2 document with this(

$('select').select2('readonly',true);

OR

$('select').prop('readonly',true);

can readonly. but it can not work for me. why?? :-(

1

There are 1 best solutions below

0
حمیدرضا بحری On BEST ANSWER

I found a solution for my problem with select2 readonly.

First, add this CSS:

select[readonly].select2-hidden-accessible + .select2-container {
    pointer-events: none;
    touch-action: none;
    opacity:0.6;
    cursor:no-drop;
}

Now to do readonly, I must add this jQuery code:

$("#Ostanid").attr("readonly", "readonly");

And to remove the readonly attribute, I must add this:

$("#Ostanid").removeAttr("readonly");