2012-07-30 3 views
0

jqGrid에 드롭 다운 (선택) 열이 있습니다. 선택 항목의 옵션이 변경되면 일부 유효성 검사를 실행해야합니다. change 이벤트가 발생했지만 선택한 옵션을 얻는 데 필요한 구문을 이해할 수 없습니다. 일반적으로이 이와 simple입니다 :미리 요소 ID를 모른 채 옵션 : 선택을 사용할 수 있습니까?

$("#someDropDownId option:selected").text(); 

나는 런타임에 드롭 다운의 ID를 만들 수도 있지만, 내 인생 선택한 텍스트를 얻을 방법을 알아낼 수 없습니다.

var rowId = $("#grid").jqGrid('getGridParam', 'selrow'); 
var selectId = rowId + '_Description'; 
//selectId is the ID of the select element, how do I get the selected value now?? 

나는 예를 들어, $("selectId option:selected").text();를 들어, 조합의 모든 방법을 시도했습니다,하지만 난 그것을 알아낼 수 없습니다. 가능합니까, 그렇다면 구문은 무엇입니까? 이벤트 핸들러 내부에있는 경우

답변

2

당신은 아마도 단지 on() 방법을 사용할 수 있습니다 수행

$('select').on('change', function(e){ 
    var selectedOptionText = $(this).find('option:selected').text(); 
}); 

JS Fiddle proof of concept합니다.

또는, 더 간단하게 :

$('select').on('change', function(e){ 
    var selectedOptionText = $(this).find('option').eq(this.selectedIndex).text(); 
}); 

JS Fiddle proof of concept.

1

당신은 $(this).val();

또는 텍스트 $(this).find("option:selected").text();을 할 수 있습니다.

0

대신 $("selectId option:selected").text()$("#selectId").val()

관련 문제