2012-09-21 5 views
-4

가능한 중복 :
Check if option is selected with jQuery, if not select a default이 선택되어 있는지 확인합니다

내가 가진 HTML과 같은 : 옵션 선택 박스를 모두 선택되어 있는지 확인하는 방법

<div id="somedivid"> 

<select class="someclass"> 
<!-- options --> 
</select> 

<select class="someclass"> 
<!-- options --> 
</select> 

</div> 

?

EDIT : i can not change html. 그래서 요소를 선택하기 위해 ID를 추가 할 수 없습니다.

+1

내가 질문 주제 + 'JQuery와'구글, 그리고 [내가 뭘 찾았]하기 (HTTP를 썼다 무슨 짓을했는지 : //stackoverflow.com/questions/149573/check-if-option-is-selected-with-jquery-if-not-select-a-default) – Tom

+0

@Tom 글쎄, 하나의 선택 요소가있을 때 해결책을 찾았습니다. . 나는 그것을하는 방법을 안다. – SomeoneS

답변

2

선택에는 항상 선택 항목이 있습니다.

그러나이 같은 빈 선택 넣어 랬 :

if ($('#select1').val() && $('#select2').val()) { 
    // both have selection 

편집 :

​<select id=select1> 
    <option></option> 
    <option>A</option> 
    <option>B</option> 
</select>​ 

당신은이를 테스트 할 수 있습니다 당신은 ID 또는 이름이없는 경우, 당신은이를 사용할 수를 :

var allSelected = true: 
$('#somedivid select').each(function() { 
    if (!$(this).val()) { 
     allSelected = false; 
     break; 
    } 
}); 
+0

그래, 나도 알아,하지만 내 선택 요소는 ID를 가지고 있지 않다, 두 요소 모두 동일한 클래스. 그리고 나는 html을 바꿀 수 없다. – SomeoneS

+0

ID 또는 이름이없는 경우 솔루션을 위해 편집했습니다. 그러나 무언가가 선택된다는 의미가 무엇인지 정의해야합니다 (예 : 비어있는 선택). –

0
<script type="text/javascript"> 
    $(function() { 
     $('#somedivid > select').bind('change', function(){ 
      if($('#sel1').attr('value') != 'default' && $('#sel1').attr('value') != 'default'){ 
       //do something here. 
      } 
     }); 
    }); 
</script> 
<div id="somedivid"> 

    <select id="sel1"> 
     <option value="default" selected="selected"></option> 
     <!-- options --> 
    </select> 

    <select id="sel2"> 
     <option value="default" selected="selected"></option> 
     <!-- options --> 
    </select> 

</div> 
0

고려 봇 시간 선택 박스는 시도 = "없음"

var isBothSelected=0; 
if($('#selectbox1 option:selected').val() =='none'){ 
    alert('Plase select the select1'); 
    isBothSelected=isBothSelected+1; 
    //return false; 
} 
if($('#selectbox2 option:selected').val() =='none'){ 
    alert('Plase select the select2'); 
    isBothSelected=isBothSelected+1; 
    // return false; 
} 
if(isBothSelected ==2){ 
    alert('Both are not selected') 
} 
0

몇 가지 기본 옵션 값이없는이

 var val= $("#selectid option:selected").value(); 
      if(val==="") 
       { 
      alert("your code on not selected condition"); 
      } 
관련 문제