<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>javascript中select的获取选中项的索引</title>
</head>
<body>
<select id="repTile" onchange="get_index()">
<option value="0">---请选择---</option>
<option value="奇葩">奇葩</option>
<option value="天地网">天地网</option>
<option value="qipa250">qipa250</option>
<option value="www.qipa250.com">www.qipa250.com</option>
</select>
<script>
function get_index() {
//获取select对象
var myselect = document.getElementById("repTile");
//获取选中项的索引
var index = myselect.selectedIndex;
alert("index=" + index);
//获取选中项索引对应的值
var selectValue = myselect.options[index].value;
alert("值:" + selectValue);
var selectText = myselect.options[index].text;
alert("selectText值:" + selectText);
}
</script>
</body>
</html>
学习在javascript中select下拉获取选中项的索引
于 2022-09-30 11:08:25 首次发布