简体   繁体   English

获取用户在选择中选择的选项的ID

[英]Get the id of an option chosen in a select by a user

I'm tryin to get the id of an option chosen in a select by a user. 我正在尝试获取用户的选择中选择的选项的ID。

$(document).ready(function(){         
    $(".my_select_class").change(function(){
        var d = $(this).find('option:selected').attr('id');
        alert(d);

        return false;       
    });
});

The id exists cause I display it in my php code but the alert open a white frame without any character, I don't understand why. 该ID存在是因为我在PHP代码中显示了该ID,但是警报打开了一个白色框,没有任何字符,我不明白为什么。 Maybe because "attr('id')" doesn't work with "find('option:selected')" ? 也许是因为“ attr('id')”与“ find('option:selected')”不兼容吗?

your code just works for me. 您的代码只对我有用。 see http://jsfiddle.net/QhXwT/ 参见http://jsfiddle.net/QhXwT/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM