开发者

Making a select choice result in a popup in javascript

So this is using javascript and HTML.

For example:

<select name='test' >
    <option value='1'>
    <option value='2'>
    <option value='3'>
</select>

If someone chooses option 开发者_如何学编程value 2 (from the dropdown) I want a popup to appear. However if they choose option value 1 or option value 3 (from the dropdown) I want nothing to happen.

How can I do this?

Thanks


Add an id to the select (<select name='test' id='test'>). Then add (after the <select>):

<script>
document.getElementById("test").onchange = function(){
    if (this.options[this.selectedIndex].value == '2') {
        alert('hello world!');
    }
}
</script>


You can do an event handler like this:

<script type="text/javascript">
function pop(a) {
    if (a.value==2) alert('two');
}

</script>

and for the html:

<select id="sel" onchange="pop(this)">
<option value="1">one</option>
<option value="2">two</option>


With something like this:

<select name='test' onchange='if(this.value==2) alert("TEST")'>
    <option value='1'>
    <option value='2'>
    <option value='3'>
</select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜