简体   繁体   中英

What is wrong with selecting with my drop down element

<html>
    <head>
        <script type='text/javascript' language='javascript' src="http://localhost/javascript/jquery-1.4.2.js">
        </script>

        <script type='text/javascript' language='javascript'>
        $(document).ready(function(){
            $("#button").mousedown(function(){
                    dropDownMenu    = $("#dropDownMenu");
                    alert(dropDownMenu.options[0].text);
                });
        });


        </script>
    </head>
    <body>
    <select id="dropDownMenu"><option>Test</option></select><br>
    <input id="button" type="button">
    </body>
</html>

Try using the text() function:

$("#button").mousedown(function() {
    var selectedItemText = $('#dropDownMenu :selected').text();
    alert(selectedItemText);
});
Your code dropDownMenu    = $("#dropDownMenu");
alert(dropDownMenu.options[0].text);

Here dropDownMenu is a JQuery object. So dropDownMenu.options is not defined. Use dropDownMenu[0] or dropDownMenu.get(0) to get the first DOM element which is a

<select>...</select> 

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