-1

I have a Select Option which I am disabling through javascript like below

document.getElementById('CmbDepartment').disabled = true;

But after disabing what happens is, the value in the database is saved as 0.

And after removing the disable part my value is getting saved properly.

But I want to disable the Select list.

Here is my HTML of the Select list

<select id="CmbDepartment" runat="server" style="width: 25%" onchange="FunEmpFill()">
     <option value="0">--Select--</option>
</select>

So is there any other way to deal with this ??

Sarath S Menon
  • 2,168
  • 1
  • 16
  • 21
Nad
  • 4,605
  • 11
  • 71
  • 160

2 Answers2

0

If I understand properly, you have assigned option value as 0. and then disabled select tag, so it will take default option value.

You can change you option value or remove completely.

Or you can dynamically assign value to select tag and selected via JS

Ex

  1. $('#CmbDepartment').append($('<option>', {value:1, text:'One'}));
  2. $('#CmbDepartment').append('<option val="1">One</option>');
  3. var option = new Option(text, value); $('#CmbDepartment').append($(option));
kamal pal
  • 4,187
  • 5
  • 25
  • 40
Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34
0

Let me propose a hack. Instead of disabling the select, I have set the style property pointer-events to none. Also, I added opacity: 0.5; to make it look disabled. This would not stop the select control from being submitted along with the other form elements.

Try running the code snippet below.

.disabled {
  pointer-events: none;
  opacity: 0.5;
}
<select>
  <option>Value 1</option>
  <option>Value 2</option>
  <option>Value 2</option>
</select>

<button onClick="document.getElementsByTagName('select')[0].classList.add('disabled');">Disable Select</button>
Nikhil Girraj
  • 1,135
  • 1
  • 15
  • 33