简体   繁体   中英

How to fire click event on select drop down

I have following code for drop down select showing drop down list using text and values.i google many codes as possible but none of them worked.what i wanted to show the selected drop down menu item in alert dialog.I already tried jquery and javascript codes but they are not working please help me to out of this problem

  <div class="col-sm-1"></div>
            <div class="col-sm-3">
                <div class="fleft " id="drop-down">

                    <select id="cd-dropdown" class="cd-select"  >

                        <option value="1" >Home</option>
                        <option value="2" >Movies</option>

                        <option value="3" >T.V. Shows</option>
                        <option value="4" >Photos</option>
                        <option value="5" >Site Help</option>

                    </select>
                </div>
            </div>

Using JQuery, you could do something like this:

$('#cd-dropdown').bind('change', function () {
   alert($(this).find('option:selected').text());
});

If you want the value instead of the text, use .val()

on changing

$(document).ready(function(){
    $("#cd-dropdown").change(function(){
       alert($(this).find('option:selected').val()); // val or text
    });
});

or on loading

$(document).ready(function () {
     alert($("#cd-dropdown").find('option:selected').val()); // val or text
});

Updated link

HTML code:

 <div class="col-sm-1"></div>
                <div class="col-sm-3">
                    <div class="fleft " id="drop-down">

                        <select id="cd-dropdown" class="cd-select"  onmousedown="this.value='';" onchange="jsFunction(this.value);" >

                            <option value="1" >Home</option>
                            <option value="2" >Movies</option>

                            <option value="3" >T.V. Shows</option>
                            <option value="4" >Photos</option>
                            <option value="5" >Site Help</option>

                        </select>
                    </div>
                </div>

JS code:

function jsFunction(value)
{
    alert(value);
}

You can do so using jQuery like this

$(document).on('change',"#cd-dropdown", function(){
 alert($(this).val());  // will display selected option's value
 alert($(this).find('option:selected').text()); //will display selected option's text
});

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