Include only the first option in the selection box

I have a “selection box” on my page, and I don’t want the user to select any option, but the first one (please do not ask me why). I cannot change for "input" with the attribute "readonly", and I cannot delete the parameters. I'm sorry for the limitations, but it should be so.

I tried this:

 $('select').attr('disabled',true);

But it happens that I do not want to "lose" the meaning. I have an ajax call that uses these values, and when I turn it off, the value "doesn't exist" anymore.

I also tried:

 $('select').click(function(){
      return false;
 });

But without success.

+3
source share
5 answers

You can do this with a single line of code.

$('option').attr('disabled','disabled').eq(0).removeAttr('disabled');

http://jsfiddle.net/yaGJc/1/

:gt() , , , .

$('option:gt(0)').attr('disabled','disabled');

http://jsfiddle.net/yaGJc/2/

+6

jquery:

$('#selector option:gt(0)').attr('disabled', 'disabled');

HTML :

<select multiple>
    <option>test1</option>
    <option disabled>test2</option>
    <option disabled>test3</option>
    <option disabled>test4</option>
    <option disabled>test5</option>
    <option disabled>test6</option>
</select>
+1

Maybe you can try this

$('.select').attr('disabled', 'disabled');

I fought this before ...

0
source
var firstValue = $('select:first').val();
$('select:first').attr('disabled', true);

Then use the firstValue variable in the ajax call.

0
source

not sure if you want to do something like tat, but you can try

$("select option").each(function (index, obj) {
    if( index > 1 )
        $(obj).attr("disabled", "disabled");
});
0
source

All Articles