7

on selecting values from the drop down, if the selected values equals to a certain value, i want the dropdown to change to readonly, how can i do that?

HTML:

<select id="s_id">
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="opel">Opel</option>
   <option value="audi">Audi</option></select>

Script:

$(document).ready(function () {
   $("#s_id").change(function () {
     var x = $(this).val();
     //alert("something's changed");
     alert($(this).val());
     if (x === opel) {
         alert("iff only.....");
         $(this).attr("readOnly", "true");
    }
   });
});

Fiddle: http://jsfiddle.net/L4reds/xKQUd/2/

L4reds
  • 412
  • 2
  • 5
  • 21
  • 2
    Firstly, you need to add quotes around the value condition: `if (x === "opel")...`. Secondly and more importantly, `select` elements cannot be made `readonly`. They can be `disabled`, but their value would then not be sent with the form data. – Rory McCrossan Jul 26 '13 at 12:15
  • 1
    With "read only" you mean that value can't be selected? – Max Markson Jul 26 '13 at 12:15
  • Your line `if (x == opel) {`, variable `opel` is _undefined_. You probably wanted to compare against _String_; `if (x == 'opel') {` – Paul S. Jul 26 '13 at 12:15
  • also use `prop` instead of `attr` – Arun Killu Jul 26 '13 at 12:18

8 Answers8

13

You need to compare x to a string of "opel" and you can use the attribute disabled, see fiddle here: http://jsfiddle.net/xKQUd/3/

To clarify: select elements cannot be set to readOnly, need to use the disabled attribute

To get around the not sending to the server issue, set a hidden input equal to the disabled value that will be sent on form submission instead, view this fiddle here: http://jsfiddle.net/xKQUd/25/

verbanicm
  • 2,406
  • 1
  • 14
  • 9
2

In your code, you used opel as a variable. but it's not a variable it's a string. So you need to take as a string (in single or double quotes).

$(document).ready(function () {
    $("#s_id").change(function () {
        var x = $(this).val();            
        alert($(this).val());
        alert("x=======" +x);
        if (x == "opel") {
            alert("iff");
            $(this).attr("disabled", "disabled");
        }
    });
});

Try this jsfiddle

or take a variable var opel = "opel";.

Try jsfiddle

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
2

Select elements can't be set readonly, but can be disabled instead. But in this case, their value won't be submitted with the form.

You have two options here.

One is to set a hidden input with the selected value, and then disable the select:

$(document).ready(function () {
   $("#s_id").on("change", function () {
       if ($(this).val() === "opel") {
           $("#myHiddenField").val($(this).val());
           $(this).attr("disabled", "disabled");
       }
   });
});

The other one, is to remove all the other options from the select, leaving just the selected one available:

$(document).ready(function () {
   $("#s_id").on("change", function () {
     if ($(this).val() === "opel") {
         $(this).children("option").each(function() {
            if (this.value != "opel")
               this.remove();
         });
     }
   });
});

It's up to you now.

emerson.marini
  • 9,331
  • 2
  • 29
  • 46
1

Select doesn't have attr ReadOnly - instead it can be disabled so your code:

$(document).ready(function () {
    $("#s_id").change(function () {
        var x = $(this).val();
        //alert("something's changed");
        alert($(this).val());
        alert("x=======" +x);
        if (x == "opel") {
            alert("iff");
           // $(this).attr("readOnly", "true");
            $(this).attr('disabled',true);
        }
    });
});

btw open is not a variable, but value, so needs to be in quotes "opel" and fiddle: http://jsfiddle.net/xKQUd/5/

Elen
  • 2,345
  • 3
  • 24
  • 47
0

Try

if (x == 'opel') {
        alert("iff");
        $(this).attr("disabled", "true");
    }

FIDDLE Demo

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
0

Try this,

if (x == "opel") {

            $(this).attr("disabled", "disabled");
        }
Jeet Bhatt
  • 744
  • 1
  • 7
  • 22
0

Try Below Code:

<select id="s_id">
 <option value="volvo">Volvo</option>
 <option value="saab">Saab</option>
 <option value="opel">Opel</option>
 <option value="audi">Audi</option>
 </select>

Jquery Code:

$(document).ready(function () {
$("#s_id").change(function () {
 var x = $(this).val();    
 if (x == 'opel') {
     $(this).attr('disabled','disabled');
}
});
});

http://jsfiddle.net/Ea6NN/

Nitin Chaurasia
  • 253
  • 1
  • 5
0

Here you are http://jsfiddle.net/SteveRobertson/xKQUd/24/

      $(document).ready(function () {
          y=1;
          $("#s_id").change(function () {
             var x = $(this).val();
    //alert("something's changed");


             if (x == "opel" && x!=y) {           
                y=x;
                $("#s_id").prop("disabled", true).change();            
    }
});

});

Four_lo
  • 1,150
  • 10
  • 28