SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question on select one option!

    Look at this html.

    HTML Code:
    <select name="cantidad" class="input_text_small">
    <option value="1" onChange="javascript:window.location='compra.php?accion=cambiar_cantidad&compra_registro=1'">Opcion 1</option>
    <option value="1" onChange="javascript:window.location='compra.php?accion=cambiar_cantidad&compra_registro=2'">Opcion 2</option>
    </select>
    When some option is selected, a javascript must be executed. As you see, the example doent' works for me, but that can give you an idea. The Java of Option 1 sends "compra_registro=1" and the 2 "compra_registro=2",

    Thanx!

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't work because you have it in the wrong place it should be:

    Code:
    <select onchange"SomeJavaScript()" ...
    ...
    ...
    ...
    </select>
    You should create routines instead typing all that code in the lines, to get the OPTION number you need to use the INDEX of the selected item.

    Look at this example:

    PHP Code:
    <SCRIPT TYPE="text/javascript">

            function 
    YooHoo() {

                     
    Text document.getElementById("ItemBox")

                     
    TheSelection "The item selected is " Text.options[Text.selectedIndex].text;

                     
    alert(TheSelection);

            }

    </SCRIPT>


    <SELECT ID="ItemBox" onChange="YooHoo()" size="1"> 

    <OPTION VALUE="1">Item 1</OPTION>
    <OPTION VALUE="2">Item 2</OPTION>
    <OPTION VALUE="3">Item 3</OPTION>
    <OPTION VALUE="4">Item 4</OPTION>

    </SELECT> 
    EDIT TO ADD: If you want the value, then replace this line:

    from:
    PHP Code:
    TheSelection "The item selected is " Text.options[Text.selectedIndex].text
    to:
    PHP Code:
    TheSelection "The item selected is " Text.options[Text.selectedIndex].value

  3. #3
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, you are right, but in fact i need to send the function the same value that i am selecting. Tha's why it's different the javascript in the options.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've edited my post, check it out. Hope that helps.

  5. #5
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    We are very near, but, in fact, I need to know the value and the field name, because the numer of selects are dynamic.

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so do this: (I've added the "name")

    PHP Code:
    <SCRIPT TYPE="text/javascript">

            function 
    YooHoo() {

                     
    Text document.getElementById("ItemBox")

                     
    TheSelection "The item selected is " Text.options[Text.selectedIndex].text;

                     
    alert(TheSelection);
                     
    alert("The name is " Text.name);

            }

    </SCRIPT>


    <SELECT ID="ItemBox" onChange="YooHoo()" size="1" name="Cantidad"> 

    <OPTION VALUE="1">Item 1</OPTION>
    <OPTION VALUE="2">Item 2</OPTION>
    <OPTION VALUE="3">Item 3</OPTION>
    <OPTION VALUE="4">Item 4</OPTION>

    </SELECT> 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •