0

I've got option select. How to set as var selected select automaticly?

<option>
<select>1</select>
<select>2</select>
</option>

E.g. I manually select option 1, and it automaticly is

<select selected="selected">1</select>

EDIT I found out.

<select id="sel">
    <option value="1">aa</option>
    <option value="2">bb</option>
    <option value="3">cc</option>
</select>

$("#sel").change(function(){
   alert($(this).val())
});
beetle
  • 193
  • 2
  • 4
  • 11

4 Answers4

1

try this

$("#myOption").val("1");
Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
  • I've got form and I need to automaticly pass the data. User can choose "2" and option "2" have to automaticly set to "selected" – beetle Apr 11 '12 at 08:20
1

your code should be

<select>
    <option selected="selected">1</option>
    <option>2</option>
</select>
ncremins
  • 9,140
  • 2
  • 25
  • 24
  • Yes - but I need this to set automaticly when i select it. I've got form. I need to pass value by ajax. – beetle Apr 11 '12 at 08:22
  • then use the jQuery mentioned in other answers $("#myOption").val("1"); wrap it in a $(document).ready() – ncremins Apr 11 '12 at 08:46
0

I agree with @creminsn!! and For that to get value Try this

var val;
$('#selectID').click(function(){
val=$('#selectID option:selected').val();
});

val will give you the selected val.And if you need text you can do

$('#selectID option:selected').text();
mesimplybj
  • 639
  • 1
  • 5
  • 28
0

I don't quite see what your issue is, however im getting the idea from what you've written that you want to submit the form upon the user selecting an option from the dropdown?

In which case u need JQuery to implement a listener (.live()) on the dropdown which will fire an event, in your case you want to submit the form (i think) so perhaps use .post() if you do want to use ajax.

look these up on the jQuery site.

dapperwaterbuffalo
  • 2,672
  • 5
  • 35
  • 48