6

I want to set the drop down as readonly. For that I user the code readonly="readonly".But its not working in asp.netmvc4.How can I solve this problem?Please help

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84
  • @Agent1 [http://stackoverflow.com/questions/8100351/how-to-make-a-dropdown-readonly-using-jquery](http://stackoverflow.com/questions/8100351/how-to-make-a-dropdown-readonly-using-jquery) Here you can find the answer. – Praveen May 23 '13 at 12:09

4 Answers4

11

You can do like this

Make other options disabled except the current one.

$('option:not(:selected)').attr('disabled', true);

See Demo

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
  • 1
    very useful tip, specially when you have to post your values from protected fields without any hidden fields $(function () { $("select[readonly='readonly'] option:not(:selected)").attr('disabled', true); }); – Jerzy Gebler Sep 16 '14 at 13:07
4

You can use .attr() in jQuery

$('#id').prop("disabled", true); 
PSR
  • 39,804
  • 41
  • 111
  • 151
0

It can be done via jquery. It saves you from browser compatibility issues too. jQuery:

$('#yourselectid').attr("disabled", true);
edd
  • 1,356
  • 7
  • 12
0

Unfortunately the select element doesn't accept readonly, W3 spec is here for reference.

As others have suggested, your best bet would be to use the disabled 'disabled' attribute instead.

johnkavanagh
  • 4,614
  • 2
  • 25
  • 37