select获取到option的value和text方法

时间:2022-06-26 20:36:27
 

function
getSelectval(id){ var selId = document.getElementById(id); //获取select的id var seleIndex =selId.selectedIndex; //获取被选项的引索值 alert(selId.options[seleIndex].value); //获取被选项的value
    alert(selId.options[seleIndex].text); //获取被选项的文本 }
 
 
<select id="sel" onchange="getSelectval('sel')">
        <option selected value="1">val1</option>
        <option value="2">val2</option>
        <option value="3">val3</option>
    </select>