tags:

views:

53

answers:

4

Hi I am new to jquery I have the following field

 <select  name[]="post">
              <option value="op1">op1</option>
              <option value="op2">op2</option>
              <option value="op3">op3</option>
              </option>
 </select>   
 How to get the post[0] & post[1] values in Jquery any Idea 
 . 
+1  A: 

you are searching for this...

<select name="my_select" id="foo">
              <option value="op1">op1</option>
              <option value="op2">op2</option>
              <option value="op3">op3</option>
              </option>
 </select> 


var options = $('#foo').find('option');
console.log($(options[0]).attr('value'));
helle
It should be noted that console.log will not work in IE.
Andir
unless you have activated the developer tools console...it will not work in ff, too if you haven't installed firebug ;-)
helle
A: 

if you want multiple select option use the key word "multiple"

<select name="my_select" id="foo" multiple>
          <option value="op1">op1</option>
          <option value="op2">op2</option>
          <option value="op3">op3</option>
          </option>
</select>
Crae
According to standard, it's: multiple="multiple"
Andir
A: 

You can get Select's selected value in the following easy ways

$('select#your_id').val();

$('select#your_id').children("option:selected").val()

$("select#your_id > option:selected").val()

And to get any option on the bases of index, you can use the following

$("select#your_id > option:nth-child(1)")

Point to note is the index starts from 1-n (0 will return empty)

Salman Riaz
A: 
<select name="test" id="test">
          <option value="op1">op1</option>
          <option value="op2">op2</option>
          <option value="op3">op3</option>
          </option>
</select> 

to get the vallue from the select list you can use this piece of code: output = $('#test').val();

$('#some_button').click(function()  {
  output = $('#test').val(); 
});
Jaison Justus