为什么我无法获得我的< select>的正确值领域? [英] Why I can not get the correct value of my <select> field?

查看:92
本文介绍了为什么我无法获得我的< select>的正确值领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 index.html 中,我有一个选择下拉菜单:

In my index.html, I have a selection drop down menu:

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

我的js:

var c = $('#car');

var selection;

c.change(function(){

    if(c.val()==='bmw'){

        selection = 'BMW';

    }else if(c.val()==='toyota'){

        selection = 'TOYOTA';

    }

});

console.log(c.val());
console.log(selection);

首次加载页面时,初始选择是宝马,我得到了控制台输出宝马这很好,然后我选择 TOYOTA ,但两个控制台输出仍然是宝马

When page firstly loaded, the initial selection is BMW, and I got console output "BMW" which is fine, then I select "TOYOTA", but the two console outputs are still "BMW".

如何在jQuery .change(...)之外获取选择的当前
值选择更改后的功能??

How to get the current value of the selection outside jQuery .change(...) function after the selection has been changed??

_ __ _ __ _ __ 拼写错误已修复 __ _ __ _ _

_________typo have been fixed_______

car.val()选择都是我的拼写错误发布,在我的代码中我使用选择,以及 c.val()

car.val() and Selection are both my typo here in the post, in my code I do use selection, and c.val().

我的观点是如何将当前选择值排除在jQuery change()函数之外。请不要再讨论我的拼写错误。谢谢。

My point is how to get the current selection value out side jQuery change() function. Please do not discuss on my typo any more. Thank you.

推荐答案

您应该使用小写来定义var选择,如下所示:

You should define your var selection with lowercase, as this:

var selection;

为什么? Javascript区分大小写,因此,敏感和敏感是不同的变量。

Why? Javascript is case sensitive, so, Sensitive and sensitive are distinct variables.

此外,您应该定义您的汽车变量,如下所示:

Also, you should define your car var, like this:

var selection;
c.change(function(){
    var car = $(this); //declare it
    if(car.val()==='bmw'){
        selection = 'BMW';
    }else if(car.val()==='toyota'){
        selection = 'TOYOTA';
    }
});

这篇关于为什么我无法获得我的&lt; select&gt;的正确值领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆