Get select element value on event using pure JavaScript

后端 未结 3 827
自闭症患者
自闭症患者 2021-01-02 00:51

I want to get the value of a select element when the select element is clicked! I want to do it without an onchange attribute in the HTML.

I would like

3条回答
  •  别那么骄傲
    2021-01-02 01:30

    ​document.getElementById('test').onchange = function() {
        alert(this.options[this.selectedIndex].val​​​ue);
    };​
    

    Does the above the work?

    Edit:

    Edited to reflect your select id's etc.

    var select_element = document.getElementById('product-form-user-enquiry-type');
    
    select_element.onchange = function(e){
        if (!e)
            var e = window.event;
        var svalue = this.options[this.selectedIndex].value;
        alert( svalue );
    }​
    

    See it working at: http://jsfiddle.net/gRoberts/4WUsG/

提交回复
热议问题