tags:

views:

372

answers:

2

Hi, I'm trying to get the value and text of multiple select boxes and add them to a hidden fields, can any one please help.

<form>  
 <select  class="select" name="select" id="select">
            <option value="0">0</option>
            <option value="1.99">1</option>
            <option value="1.99">2</option>
            <option value="1.99">3</option>
            <option value="1.99">4</option>
            <option value="1.99">5</option>
            <option value="1.99">6</option>
            <option value="1.99">7</option>
            <option value="1.99">8</option>
          </select>

   <select class="select" name="select2" id="select2">
          <option value="0">0</option>
          <option value="2.99">1</option>
          <option value="2.99">2</option>
          <option value="2.99">3</option>
          <option value="2.99">4</option>
          <option value="2.99">5</option>
          <option value="2.99">6</option>
          <option value="2.99">7</option>
          <option value="2.99">8</option>
        </select>

    </form>

   $(function() {

//function to go through all of the select boxes and get their values based on their change state.

var qty=0;var price=0;items="";
$(".select").each(function() {

 $(this).change(function(){
  qty =($(this).text());
  price = (($(this).val()) * qty);
  items = ($(this).prevAll(".descriptionsColor").text() );
 });

});



//function to add items to shopping cart when add button is clicked.
$('#my-add-button').click(function() {

 var randomNumber = Math.floor((Math.random() * 9000)+200);
 $('input[name=my-item-id]').val(randomNumber);
 $('input[name=my-item-name]').val(items);
 $('input[name=my-item-price]').val(price);


});

I need to get the value and text of the select boxes on change and multiply them to get a price, also get the description of the item which is in a span tag with a class.

Do I need to use array variables to store the values and text and prices?

thanks for any help.

+1  A: 

selecting the selected value can be achieved like this:

$("#Select1").val();

selecting the text from the selected item can be done this way:

$("#Select1 option:selected").text()

so you would say

qty = $(this).find('option:selected').text();
price = $(this).val();

then you have the correct qty and price you can multiply them and print it somewhere

Sander
A: 

To Read Select Option Value

$('#selectId').val();

To Read Selected Text

$('#selectId>option:selected').text();
rahul
I have a lot of select boxes in one page, can I not iterate through them with the given class and get the required value and text on their change state. thanks
amir