DropDown에서 선택한 데이터 ID를 얻는 방법은 무엇입니까?[복제하다]
-
21-12-2019 - |
문제
thymeleaf를 사용하여 다음 코드를 작성했습니다.
<div class="form-group">
<div class="col-xs-12 col-sm-9">
<div class="clearfix">
<label
class="control-label col-xs-12 col-sm-4 no-padding- right"
for="name">System</label> <select
class="tbrightspace input-large" id="userCategory"name="systemId"
style="margin: 0px 0px 10px 0px; padding: 0px; width:42%;">
<option th:text="--Select--">Select</option>
<option th:each="roles:${systemList}"
th:value="${roles.systemId}"
th:selected="${roles.systemName}"
th:text="${roles.systemName}" />
<!-- <option value="101">Collections Management</option>
<option value="102">CHD</option>
<option value="103">Client Tools</option> -->
</select>
</div>
</div>
</div>
이 DropDown 상자에는 세 개의 데이터가 로드됩니다. 이제 선택한 데이터 ID를 가져오고 싶습니다. 예를 들어 ajax URL에 ID를 전달하여 DB 테이블의 모든 데이터를 찾고 싶기 때문입니다.
$.ajax({
url: "/collection-ui/api/permissions/findall/id", //http://localhost:8080
success: function( treeData ) {
var tree = $("#tree2").dynatree("getTree");
var rootNode = $("#tree2").dynatree("getRoot");
나는 다음과 같이 시도했다
<script th:inline="javascript">
/*<![CDATA[*/
var id=/*[[@{{id}(id=${systemList.get(0).systemId})}]]*/
alert(id);
/*]]>*/
</script>
이것은 경고이거나 첫 번째 개체 ID 데이터만 제공합니다. 하지만 드롭다운 상자에서 데이터를 선택하는 동안 ID를 얻고 싶습니다. 어떻게 해야 합니까??누구든지 도와주세요
해결책
이 시도
$("#dropdown").on('change',function(){
var getValue=$(this).val();
alert(getValue);
});
또 다른 접근법:
$('#dropdown').change(function(){
var id = $(this).find('option:selected').attr('id')
})
다른 팁
이것은 거의 중복되지만 오래된 게시물
하지만 당신은 원해요 ID
그래서 이런 것을 시도해 볼 수 있습니다 ..
HTML:
<select id="ddlViewBy">
<option id="s1" value="1">test1</option>
<option id="s2" value="2" selected="selected">test2</option>
<option id="s3" value="3">test3</option>
</select>
<a name="ding" onclick="test()">Click me!!</a>
자바스크립트:
function test(){
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].id;//text
alert(strUser);
return false;
}
여기 일하고 있어요 jsFiddle
제휴하지 않습니다 StackOverflow