카테고리 없음

jquery 셀렉트박스 event 발생 코드

728x90

db에서 불러온 department를 셀렉트 박스로 선택했을 때 Jquery를 통해 id값 받아오는 법

<div class="hr-line-dashed"></div>
<div class="form-group">
    <label class="col-sm-2 control-label labelTitle"><span class="">부서</span></label>
    <div class="col-sm-10">
        <div class="selects">
            <select class="form-control" name="selectDep" id="selectDep">
                <c:forEach items="${department}" var="dep">
                    <c:if test="${dep.getParent() eq -1}">
                        <option value="${dep.id}"> ${dep.name}</option>
                    </c:if>
                </c:forEach>
            </select>
            <span id="depart-no" style="display: none; color: #d92742; font-weight: bold;">부서를 선택해주세요.</span>
        </div>
    </div>
</div>

 

$("#selectBox").on("change", function(){
    //selected value
    $(this).val();
    $("option:selected", this).attr("value");
    //selected option element
    $("option:selected", this);
    $("option:selected", this).text();
    $(this).find("option:selected").text();
});

 

728x90