jQuery get value of select onChange

2018-12-31 14:11发布

I was under the impression that I could get the value of a select input by doing this $(this).val(); and applying the onchange parameter to the select field.

It would appear it only works if I reference the ID.

How do I do it using this.

标签: jquery select
13条回答
妖精总统
2楼-- · 2018-12-31 14:54

Note that if these are not working, it might be because the DOM has not loaded and your element was not found yet.

To fix, put the script at the end of body or use document ready

$.ready(function() {
    $("select").on('change', function(ret) {  
         console.log(ret.target.value)
    }
})
查看更多
登录 后发表回答