<!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>浅谈select的触发事件</title> </head> <body> <select id="daidai" οnchange="displayData()"> <option value="1">这是值为1的数据</option> <option value="0">这是值为2的数据</option> </select> <script> function displayData() { var select = document.getElementById('daidai'); var content = select.options[select.selectedIndex].text; alert(content); } </script> </body> </html> 解释: 1:拿到select对象: var select = document.getElementById('daidai'); 2.拿到选中项的索引: var index=select.selectedIndex ; // selectedIndex代表的是你所选中项的index 3:拿到选中项options的value: select.options[index].value; 4:拿到选中项options的text: select.options[index].text;
浅谈select的触发事件及如何获取select选中的值
于 2022-09-12 14:37:25 首次发布