简体   繁体   中英

Dropdown menu, call one function when an item selected, call another when another item selected

I have a drop down menu, which shows some values from database. And as you can see, I have called a function callfunc() onchange of the select elements. Initially, 'Choose me to show page1' is kept selected as default. When someone selects anything else, callfunc() is called. Value of that selected item is passed onto the function. This function shows contents from another page using AJAX. This works fine. But I want to implement something else. I want to call another function when 'Choose me to show page1' is selected. I wasn't able to do that successfully. So, this is the scenario. When a user selects any other select other than 'Choose me to show page1'item from the dropdown, callfunc() is called. If 'Choose me to show page1' isselected, call another function, say.. abc();

<select name="selectme"  onchange="page2(this.value)">

    <option value="anothervalue" selected="selected">Choose me to show page1</option>

    <?php
    foreach($val_array3["from_db"] as $key3=>$value3)
    {
    $vvv=$value3['val_from_db'];
    ?>

    <option value="<?php echo $vvv ?>">Choose me to show page2</option>

    <?php } ?>

    </select>

Check this Demo Fiddle .

Your are passing this.value in page2(this.value) . So in this function,

function page2(value){

    if(value == 'anothervalue'){   //anothervalue is the value of the first default option.
        abc();
    }
    else{
        //your default function.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM