简体   繁体   中英

Why my jQuery does not return to me the value of <select>

My index.html :

<body>
   <select id="car">
    <option value="TOYOTA">TOYOTA</option>
    <option value="BMW">BMW</option>
    </select>

    <input type=button id="get_btn" value="Get"></input>

    <script src="myscript.js"></script>

</body>

myscript.js :

var btn=$('#get_btn');
btn.click(function(){

    var car= $('#car').val;
    alert(car);
});

When I press the get button, I expect an alert show the selected car, but I got an alert window with the jQuery val function code:

function (value) {
    if (!arguments.length) {
        var elem = this[0];
        if (elem) {
             ....

Where goes wrong?

".val" is a function. Use:

var car= $('#car').val();

val is a function, not a variable. You need to amend your myscript.js code as follows:

var btn=$('#get_btn');
btn.click(function(){
    var car = $('#car').val();
    alert(car);
});

you have missing brackets :

var car= $('#car').val;

and should be :

var car= $('#car').val();

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