MateuszC
MateuszC

Reputation: 499

Reading the value from select

I have around 30 of select form like this with different id(number_x):

<div class="holder">
    <select class="start" id="number_x" maxlength=5 size=1 value>
        <option value><option>
        <option value>1<option>
        <option value>2<option>
        <option value>3<option>
        <option value>4<option>
        <option value>5<option>
    </select>
</div>    
<div class="holder">
    <select class="start" id="number_x+1" maxlength=5 size=1 value>
        <option value><option>
        <option value>1<option>
        <option value>2<option>
        <option value>3<option>
        <option value>4<option>
        <option value>5<option>
    </select>
</div>

After the value from 1 to 5 is chosen I need to read it with jquery, so I can write it in table for future calculations.

--pseudocode (val() dont work)
$(button).click{
    for(x++){
        tab[x]= $('.holder .start#number_x').val();
     }
}

All I need is just to read the value from the select.start

Upvotes: 0

Views: 122

Answers (6)

vinothini
vinothini

Reputation: 2604

Please have a look at http://jsfiddle.net/2dJAN/86/

Like below you can get the selected values of all select boxes

$.each($('select'),function(){
        alert($("option:selected", this).val());
});

Upvotes: 0

billyonecan
billyonecan

Reputation: 20250

You can use the attribute starts with selector to target the relevant <select> elements. Then just store an id: value pair, like so:

var selectValues = [];
$('select[id^="number_"]').change(function() {
    selectValues[this.id] = this.value;
});

Here's a demo fiddle

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

For the various IDs that is starting from _number_x_, Try this pseudo code:

$(button).click{
    for(x++){
        tab[x]= $('select[id^=number_x]').val();
     }
}

OR

$(button).click{
   for(x++){
      tab[x]= $('.holder .start#number_x' + x '"').val();
   }
}

And for the HTML mark up

Do this

<option value=""><option>
<option value="">1<option>

Upvotes: 1

Add all your select box inside a table and you can loop through the select boxes in each form using:

$('#'+tableId+' select').each(function(){
   selected_value = ($(this).val());
   //add your logic to store it in a table for future calculations
});

Upvotes: 0

Prabhuram
Prabhuram

Reputation: 1268

You should either use

<option><option>
<option>1<option>

or

<option value=''><option>
<option value='1'>1<option>

and you can use val() function in jquery to get the values

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Your html syntax is not valid.

change

  <option value><option>
        <option value>1<option>

to

 <option value=""><option>
   <option value="">1<option>...and so on 

Then calling val() works

See Demo here

Upvotes: 0

Related Questions