简体   繁体   中英

Alert when a particular value from dropdown is selected

I have a Dropdown(select) in a website that is not controlled by me.But it can be modified by javascript,html,jquery..etc(they have provided web applet for doing so)

This dropdown can be identified an ID 'Zdd_1'. All i want if the user selects "Yes" in the dropdown then only then an alert should come. W hen the user navigates to the page. the dropdown might be Yes or some other value, but i dont care then.

I need this alert only when the state of the drop down changes from any value to "YES". I am not much into jquery and ajax, so if this can be done done simply( mean just using javascript)it would be great.

It can be easly done with the help of jQuery.

$('#Zdd_1').change(function(){
    if($(this).val() === 'yes')
        alert("Hi, I'm alert!");
});

where Zdd_1 be the id for the select element.

If you want, of course you can use only javascript.

Javascript:

    function selectChange(element) {
        if (element.options[element.selectedIndex].value == 'Yes')
            alert("Hi, I'm alert!");
    }

Html:

<select id="Zdd_1" onchange="selectChange(this)">
    <option value="Maybe">Maybe</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
</select>

Try this

    var dropdown = document.getElementById("Zdd_1");
    dropdown.onchange = function(event){
       if(dropdown.value=="Yes"){
         alert("Your message")
       }
    }

or check here working demo

On change of dropdown , you can check whether the changed value is "Yes" or not, if it is Yes then alert the text you want .

Try this

Html

<select onchange="changeddl(this)">
    <option value="Maybe">Maybe</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
</select>

javascript

function changeddl($this)
{
    if($this.value=="Yes")
    {
        alert("Whatever you want to alert!");
    }
}

You can check the fiddle here

Try something like this:

Demo

You can also call a function on onchange event.

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