jQuery get value of select onChange

后端 未结 16 1777
星月不相逢
星月不相逢 2020-11-22 05:31

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 sel

16条回答
  •  独厮守ぢ
    2020-11-22 06:07

    I was under the impression that I could get the value of a select input by doing this $(this).val();

    This works if you subscribe unobtrusively (which is the recommended approach):

    $('#id_of_field').change(function() {
        // $(this).val() will work here
    });
    

    if you use onselect and mix markup with script you need to pass a reference to the current element:

    onselect="foo(this);"
    

    and then:

    function foo(element) {
        // $(element).val() will give you what you are looking for
    }
    

提交回复
热议问题