2

I'm sure this is really simple, but I can't seem to get it working. I have a "time" select list, which has a number as "rel" attached to each option. If the user changes the time selected, I want a new list of options to display depending on what is selected. Does that make sense?

Here's my first select:

<select name="time" id="time">
  <option value="7:00am" rel="10">7:00am</option>
  <option value="12:30pm" rel="16">12:30pm</option>
</select>

If the user selects 7:00am, I want a new option list (using jquery) to give options from 1 - 10. Like this:

<select name="quantity" id="quantity">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  ............................
  <option value="10">10</option>
</select>

Here's what I have so far...

<script type="text/javascript" language="javascript">
  jQuery("#time").change(function(){
    var positions = jQuery("#time :selected").attr("rel"); //this grabs the rel from time

    //this is where it should create a list of options to append(??) to the select list.

    jQuery("#showQuantity").show(); //this shows the hidden field for quantity
  });
</script>

I hope it makes sense, but I'm stuck on it.

2
  • I'm not sure if I understand a couple of parts. Does there need to be a different list of options for each rel value? How does the rel affect the options available? Most likely there are several ways ways to accomplish what you're looking to do. Commented Mar 19, 2010 at 5:24
  • The rel for the times change, but that's done earlier on in the code.. So it's variable. There should only be one "quantity" select list, with the numbers from 1 to (whatever the selected rel value is) if that makes sense? Commented Mar 19, 2010 at 5:33

1 Answer 1

4

Below is the code you can use for adding options.

    $("#quantity").empty();//Clear options if there are any already existing ones.

    for( i=1; i<= positions; i++ )
    {

        $("#quantity").append($("<option value="+i+">"+ i+"</option>") );

    }
Sign up to request clarification or add additional context in comments.

2 Comments

I am little bit confuse about requirement but there is one more question from my side. The question is that, is it possible to add values in #quantity select from database in PHP and JSP?
Thanks Sharath for your help, worked great! @Param I can't grab it from the database as the positions change, depending on a few different things in the database, and also what the user selects on the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.