<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
데릭쉐퍼드와 값을 얻을 수 있는 옵션을 선택한 javascript: 선택한 텍스트를 볼 수 있는 방법을 아는 사람 값이거나 그것에 대한 함수를 작성할 수 있는 방법을 알려 주십시오. 그럼 어떻게 해야 해 그 이후 나는 론창레 지정받습니다 () 함수를 사용하여 선택?
이를 위해 사용하여 JavaScript 또는 포함한다.
<script>
function val() {
d = document.getElementById("select_id").value;
alert(d);
}
</script>
<select onchange="val()" id="select_id">
$('#select_id').change(function(){
alert($(this).val());
})
에 론창레 함수은 필요가 없습니다. Ms 의 값이 한 줄 수 있습니다.
document.getElementById("select_id").options[document.getElementById("select_id").selectedIndex].value;
또는 분할합니다 가독성을 높이기 위해 it up.
var select_id = document.getElementById("select_id");
select_id.options[select_id.selectedIndex].value;
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
function test(a) {
var x = (a.value || a.options[a.selectedIndex].value); //crossbrowser solution =)
alert(x);
}
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2">Communication</option>
<option value="3">Communication</option>
</select>
끝 - < 스니핏 >;!
와우, 정말 답변 중 재사용 가능한 솔루션을 아직 없습니다. 내 말은, '이벤트' 만 있는 인수와 doesn& # 39 는 스탠다트 받아야 하는 이벤트 핸들러를 사용해야 할), 침입탐지시스템 (ids) 전혀 없다. # 39 i&; d 사용:
function handleSelectChange(event) {
// if you want to support some really old IEs, add
// event = event || window.event;
var selectElement = event.target;
var value = selectElement.value;
// to support really old browsers, you may use
// selectElement.value || selectElement.options[selectElement.selectedIndex].value;
// like el Dude has suggested
// do whatever you want with value
}
Js 인라인 때마다 이 처리기에서 사용할 수 있습니다.
<select onchange="handleSelectChange(event)">
<option value="1">one</option>
<option value="2">two</option>
</select>
jQuery:
jQuery('#select_id').on('change',handleSelectChange);
또는 바닐라 JS 처리기에서 설정:
var selector = document.getElementById("select_id");
selector.onchange = handleSelectChange;
// or
selector.addEventListener('change', handleSelectChange);
예 스니핏:
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
function handleSelectChange(event) {
var selectElement = event.target;
var value = selectElement.value;
alert(value);
}
<select onchange="handleSelectChange(event)">
<option value="1">one</option>
<option value="2">two</option>
</select>
끝 - < 스니핏 >;!
<script>
function test(a) {
var x = a.selectedIndex;
alert(x);
}
</script>
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2">Communication</option>
<option value="3">Communication</option>
</select>
제가 생각하기엔 누구나 '가치' 와 '텍스트' 에서 얻을 수 있는 옵션을 대한 게시하기를 ',' 레이블 ',' 어느 누구도 < option> 제안했다.
그래서 저는 레이블 '너무' 등 거의 모든 브라우저에서 지원되는
Afaq '가치' () 와 같은 다른 제안됩니다)
function test(a) {
var x = a.options[a.selectedIndex].value;
alert(x);
}
Afaq '옵션' 'text' (즉, 통신 또는 - 선택)
function test(a) {
var x = a.options[a.selectedIndex].text;
alert(x);
}
function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2" label=‘newText’>Communication</option>
</select>
>, 참고: Html 의 값이 2 위, '옵션' 통신물 뉴트릭스트 반환되므로 레이블 '대신'
또한
>. 참고: 파이어폭스 (국한됨 반품하십시오) 의 레이블을 설정하는 것은 불가능합니다.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
function test(){
var sel1 = document.getElementById("select_id");
var strUser1 = sel1.options[sel1.selectedIndex].value;
console.log(strUser1);
alert(strUser1);
// Inorder to get the Test as value i.e "Communication"
var sel2 = document.getElementById("select_id");
var strUser2 = sel2.options[sel2.selectedIndex].text;
console.log(strUser2);
alert(strUser2);
}
<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
끝 - < 스니핏 >;!
좋겠다 나도 날잡아 샘플링합니다 설명할 수 있지만, 내 손으로 직접 도움이 된다. # 39, don& 있습니다 , t need (), 테스트 코드를 가져오는 스니핏 론창레 = " " 실행하십시오 라이브 결과.
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
document.getElementById("cars").addEventListener("change", displayCar);
function displayCar() {
var selected_value = document.getElementById("cars").value;
alert(selected_value);
}
<select id="cars">
<option value="bmw">BMW</option>
<option value="mercedes">Mercedes</option>
<option value="volkswagen">Volkswagen</option>
<option value="audi">Audi</option>
</select>
끝 - < 스니핏 >;!
<! - begin 스니핏: js 숨기십시오: 거짓값 콘솔: 진정한 바벨. > 거짓값 -;
function test(){
var sel1 = document.getElementById("select_id");
var strUser1 = sel1.options[sel1.selectedIndex].value;
console.log(strUser1);
alert(strUser1);
// Inorder to get the Test as value i.e "Communication"
var sel2 = document.getElementById("select_id");
var strUser2 = sel2.options[sel2.selectedIndex].text;
console.log(strUser2);
alert(strUser2);
}
<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
끝 - < 스니핏 >;!
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;