jQuery选择框已选中此选项

jQuery选择框已选中此选项,jquery,Jquery,我有一个选择框,我正在尝试选择当它改变时从中选择的选项。正如您在之前的脚本中看到的那样,我曾经再次调用该元素,但是我没有重用元素id名称,而是尝试选择$(this),但我不知道如何让它工作 之前 $('#maptypecontrol').change(function(){ maptypecontrolval = $('#maptypecontrol>option:selected').val(); }); 尝试代码(失败) 您可以这样做: $('#maptypecontrol'

我有一个选择框,我正在尝试选择当它改变时从中选择的选项。正如您在之前的脚本中看到的那样,我曾经再次调用该元素,但是我没有重用元素id名称,而是尝试选择
$(this)
,但我不知道如何让它工作

之前

$('#maptypecontrol').change(function(){
    maptypecontrolval = $('#maptypecontrol>option:selected').val();
});
尝试代码(失败)

您可以这样做:

$('#maptypecontrol').change(function(){
    maptypecontrolval = $('option:selected', this).val();
    console.log(maptypecontrolval);
});
这将为您提供在下拉菜单
change
事件中选择的选项的值,您可以在浏览器控制台中进行检查


或者你可以这样做:

$('#maptypecontrol').change(function () {
    maptypecontrolval = this.value;
    console.log(maptypecontrolval);
});
您可以执行以下操作:

$('#maptypecontrol').change(function(){
    maptypecontrolval = $('option:selected', this).val();
    console.log(maptypecontrolval);
});
这将为您提供在下拉菜单
change
事件中选择的选项的值,您可以在浏览器控制台中进行检查


或者你可以这样做:

$('#maptypecontrol').change(function () {
    maptypecontrolval = this.value;
    console.log(maptypecontrolval);
});

只需使用
这个

$('#maptypecontrol').change(function(){
    maptypecontrolval = $(this).val();
    alert(maptypecontrolval);
});

顺便说一句,你也可以使用
.on(change(){…})
.live(change(){…})
(当然它是不推荐的)作为页面结果。

只需使用
这个

$('#maptypecontrol').change(function(){
    maptypecontrolval = $(this).val();
    alert(maptypecontrolval);
});
顺便说一句,你也可以使用
.on(change(){…})
.live(change(){…})
(当然它是不推荐的)作为页面结果。

你可以简单地使用
$(this.val()以获取所选值,如下所示

$('#maptypecontrol').change(function(){
    maptypecontrolval = $(this).val();
});
您只需使用
$(this.val()以获取所选值,如下所示

$('#maptypecontrol').change(function(){
    maptypecontrolval = $(this).val();
});