Set Select2 to disabled or not

How to set the select2 field to readonly or selectable by condition?for example,

$("#x_local").val().on("change focusout", function() {
  if ($("#x_local").val() == 1) {
// test on below format 1 by 1 (not all together)
// {field_var} like $("#x_country"), $("#el_{table}_country")
    $("{field_var}").attr('readonly', 'readonly');
    $("{field_var}").attr('readonly', true);
    $("{field_var}").attr('disabled', true);
    $("{field_var}").attr('enable', false);
    $("{field_var}").prop('readonly', true);
    $("{field_var}").prop('disabled', true);
    $("{field_var}").prop('enable', false);
  } else {
    $("{field_var}").removeAttr('readonly');
    $("{field_var}").prop('readonly', false);
    $("{field_var}").prop('disabled', false);
    $("{field_var}").prop('enable', true);
 }
});

Some of them are able to make the field readonly. But when try to enable back, the lookup condition is GONE.What should be the correct way to make it work?TIA

According to https://select2.org/appearance, it should be, e.g. in Add page for the “Cars” table of demo project

$("#x_Trademark").prop("disabled", true); // Disable it
$("#x_Trademark").prop("disabled", false); // Enable it

This code will make the field disable. (Great)But when click Add (save), it prompt
Please enter required field - TrademarkSeems the Trademark value is reset to blank/null.Any suggestion?

You may add your code in Row_Inserting server event to assign the field value with your default value.

mobhar wrote:

You may add your code in > Row_Inserting > server event to assign the field
value with your default value.

This is a select field, no default value. I’m doing a conditional ‘disabled’ for this field.Or I need to create a temporary array / or temporary element as array (currently 4 select fields) to keep those values?

scs wrote:

But when click Add (save), it prompt
Please enter required field - Trademark

Then make sure that field is not setup as Required.

Means that all cars (demo2022) will not have any trademark if I entered the created with a value of the car right?

$("#x_amount").on("change", function() {
 $("#x_trademark").prop("disabled", true);
});

scs wrote:

This is a select field, no default value.

It actually depends on your needs or your business-logic, as you mentioned above.

My temporary solution:-$(“#el{tablename}{fieldname}”).select2().val($(“#x_{fieldname}”).val());At least it works as expected.

You should remove .select2().

[quote=arbei post_id=169566 time=1642063195 user_id=84879]
You should remove .select2().
[/quote]I’m looking for readonly features.if the .select2() removed, user can re-select a new value. This is not what I want.At least with the .select2(), user cannot select any new value.

You actually re-initiate the field with empty options. Are you sure you can re-enable the field that way?

Change it to select2(“destroy”) better right? And I don’t need to re-enable those fields in this form.

According to Select 2 doc:

The destroy method will remove the Select2 widget from the target element. It will revert back to a standard select control.

Are you sure you want to do that?What if the user revert the field which disabled the Select2 field by your code?

The condition is set to… after any one of the amount field changed, some of the reference field might not be able to change.another way is to create a new ajax to filter select data only return the value being selected… or any good suggestion?