여기 몇 가지 옵션 요소가 있습니다. 그 중 하나는 값 apple을 보유하고 있습니다. 값 app를 보유하는 option 요소의 id를 가져 오려고합니다. 다음은 다음을 수행했습니다. 나누었다는해당 값을 통해 특정 옵션 요소의 ID를 얻으십시오
<!DOCTYPE html>
<html>
<body>
<form>
Select a fruit:
<br>
<select id="mySelect" size="4">
<option id='for_apple'>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
</form>
<br>
<button onclick="myFunction()">Remove selected fruit</button>
<script>
function myFunction() {
var str="Apple";
var x = document.getElementById("mySelect");
if(x[x.selectedIndex].value == str){
alert((x[x.selectedIndex].value).parentElement.id);
}
}
</script>
</body>
</html>