옵션이 선택되었는지 확인하는 방법
$('#mySelectBox option').each(function() {
if ($(this).isChecked())
alert('this option is selected');
else
alert('this is not');
});
분명히, isChecked
작동하지 않습니다. 그래서 내 질문은 이것을하는 올바른 방법은 무엇입니까? 감사.
최신 정보
선택한 옵션에 대한 더 직접적인 jQuery 방법은 다음과 같습니다.
var selected_option = $('#mySelectBox option:selected');
질문에 대답하는 .is(':selected')
것은 당신이 찾고있는 것입니다.
$('#mySelectBox option').each(function() {
if($(this).is(':selected')) ...
비 jQuery (아마도 모범 사례) 방법은 다음과 같습니다.
$('#mySelectBox option').each(function() {
if(this.selected) ...
선택한 값만 찾으려면 다음을 시도하십시오.
$('#mySelectBox').val()
선택한 값의 텍스트를 찾으려면 다음을 수행하십시오.
$('#mySelectBox option').filter(':selected').text();
체크 아웃 : http://api.jquery.com/selected-selector/
다음에는 중복되는 SO 질문을 찾으십시오.
현재 선택된 옵션 취득 또는 설정 선택한 옵션 또는 어떻게 $ jQuery를에 (이) 선택한 옵션을 얻는 방법을? 또는 옵션 [selected = true]이 작동하지 않습니다
이 방법으로 선택한 옵션을 얻을 수 있습니다.
$('#mySelectBox option:selected')...
당신이 반복 처리 모든 옵션을 원하는 경우에, 그것을 할 this.selected
대신 this.isChecked
하는 존재하지 않습니다
$('#mySelectBox option').each(function() {
if (this.selected)
alert('this option is selected');
else
alert('this is not');
});
최신 정보:
이것을 사용하도록 제안하는 많은 답변이 있습니다.
$(this).is(':selected')
글쎄, 그것은 훨씬 빠르고 쉽게 수행 할 수 this.selected
있으므로 왜 원시 DOM 요소 메소드가 아닌 그것을 사용해야합니까?!
읽기 귀하의 DOM 속성 및 기능을 알고 에 jQuery
태그 정보
에 익숙하지 않거나 익숙하지 않은 경우 is()
의 값을 확인하면 prop("selected")
됩니다.
$('#mySelectBox option').each(function() {
if ($(this).prop("selected") == true) {
// do something
} else {
// do something
}
});
편집 :
주석에서 @gdoron이 지적했듯이 옵션의 선택된 속성에 액세스하는 가장 빠르고 적절한 방법은 DOM 선택기를 사용하는 것입니다. 다음은 이 조치를 표시 하는 바이올린 업데이트 입니다.
if (this.selected == true) {
appears to work just as well! Thanks gdoron.
Consider this as your select list:
<select onchange="var optionVal = $(this).find(':selected').val(); doSomething(optionVal)">
<option value="mostSeen">Most Seen</option>
<option value="newst">Newest</option>
<option value="mostSell">Most Sell</option>
<option value="mostCheap">Most Cheap</option>
<option value="mostExpensive">Most Expensive</option>
</select>
then you check selected option like this:
function doSomething(param) {
if ($(param.selected)) {
alert(param + ' is selected!');
}
}
use
$("#mySelectBox option:selected");
to test if its a particular option myoption
:
if($("#mySelectBox option:selected").text() == myoption){
//...
}
You can use this way by jquery :
$(document).ready(function(){
$('#panel_master_user_job').change(function () {
var job = $('#panel_master_user_job').val();
alert(job);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="job" id="panel_master_user_job" class="form-control">
<option value="master">Master</option>
<option value="user">User</option>
<option value="admin">Admin</option>
<option value="custom">Custom</option>
</select>
In my case I don't know why selected is always true. So the only way I was able to think up is:
var optionSelected = false;
$( '#select_element option' ).each( function( i, el ) {
var optionHTMLStr = el.outerHTML;
if ( optionHTMLStr.indexOf( 'selected' ) > 0 ) {
optionSelected = true;
return false;
}
});
If you only want to check if an option is selected, then you do not need to iterate through all options. Just do
if($('#mySelectBox').val()){
// do something
} else {
// do something else
}
Note: If you have an option with value=0 that you want to be selectable, you need to change the if-condition to $('#mySelectBox').val() != null
If you need to check option selected state for specific value:
$('#selectorId option[value=YOUR_VALUE]:selected')
참고URL : https://stackoverflow.com/questions/10213620/how-to-check-if-an-option-is-selected
'programing tip' 카테고리의 다른 글
파이썬에서 문자열의 크기를 얻는 방법? (0) | 2020.06.19 |
---|---|
bash에서 mod 연산자를 사용하는 방법은 무엇입니까? (0) | 2020.06.19 |
WPF 바인딩을 사용하여 두 개의 명령 매개 변수 전달 (0) | 2020.06.19 |
Python : TypeError : 'str'및 'int'개체를 연결할 수 없습니다. (0) | 2020.06.19 |
Mongo DB에서 저장과 삽입의 차이점은 무엇입니까? (0) | 2020.06.19 |