html select box 값 가져오기

Posted by Breeze24
2017. 8. 11. 12:42 Web

html을 사용하다 보면 약간 헷갈리는 부분이 있다. 


html select box


select box에 있는 선택된 영역의 텍스트만 가져오는 부분이다. 

아래와 같이 작성하면 선택된 콤보박스의 텍스트만 가져올 수 있다. 

.


eform.comboChk.options[eform.comboChk.selectedIndex].text



<!DOCTYPE html>

<html lang="ko">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<script language="JavaScript">

<!--

function test(){

    var eform = document.forms[0];

    alert(eform.comboChk.value);

    alert(eform.comboChk.options[eform.comboChk.selectedIndex].text);

}

//-->

</script>

</head>

<body>

<form name="eForm" method="post">

   <select name="comboChk" id="comboChk">

      <option value="1" selected>하루

      <option value="2">이틀

      <option value="3">날이 갈수록

      <option value="4">이미

      <option value="5">나에게로

      <option value="6">포도

      <option value="7">푸하

  </select>

  <a href="javascript:test();"><span>확인</span></a>

</form>

</body>

</html>


실행하면 아래처럼 결과가 나온다. 


.


셀렉트 박스