Get select element value on event using pure JavaScript

后端 未结 3 844
自闭症患者
自闭症患者 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:49

    A solution that will work with all browsers (including IE8) :

    var select_element = document.getElementById('product-form-user-enquiry-type');
    
    select_element.onchange = function() {
        var elem = (typeof this.selectedIndex === "undefined" ? window.event.srcElement : this);
        var value = elem.value || elem.options[elem.selectedIndex].value;
        alert(value);
    }​
    

提交回复
热议问题