2016-09-13 14 views
1

I持って7シンプル選択ボックス:ALL選択ボックスは、デフォルトのオプションではなく選択した何かを持っている場合すべての選択ボックスにjqueryを使用してオプションが選択されているかどうかを確認する方法?

<select> 
    <option selected disabled>choose something</option> 
    <option>some text</option> 
    <option>some more text</option> 
    <option>and more and more</option> 
</select> 

<select> 
    <option selected disabled>choose something</option> 
    <option>some text</option> 
    <option>some more text</option> 
    <option>and more and more</option> 
</select> 

<select> 
    <option selected disabled>choose something</option> 
    <option>some text</option> 
    <option>some more text</option> 
    <option>and more and more</option> 
</select> 

<select> 
    <option selected disabled>choose something</option> 
    <option>some text</option> 
    <option>some more text</option> 
    <option>and more and more</option> 
</select> 

はどうすれば確認できますか?

+0

どこオプション値はありますか? – yash

+0

'select'入力の' value'を設定します.... value属性が指定されていない場合、 'text'は' value'として扱われます.. – Rayon

+0

'document ' .getElemnetById( 'HTMLidAttribute') 'それは' Element.selected'のためのテストと同じくらい簡単です。 – PHPglue

答えて

3

長さ== 0の場合、デフォルトの要素が選択されていない場合、無効になっている選択されたオプションを見つけることができます。

if($('option[disabled]:selected').length == 0){ 
    // ALL the select boxes have something selected rather than the default option 
} 
+0

@nnnnnn:はい。最初にエラーをログに記録してから、テキストを変更しました。ありがとう:) –

0

これを試してください。 selectの値を取得し、ヌルかどうかをチェックし、ヌルの場合はカウントをインクリメントします。

$(function(){ 
 
    $('#btn').click(function(){ 
 
    var count = 0; 
 

 
    $('select').each(function(){ 
 
     
 
    if($(this).val() != null){ 
 
     count ++; 
 
    } 
 
     
 
    }) 
 
    if(count == 4) { 
 
     console.log('all selected'); 
 
    } 
 
    }) 
 
    
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select> 
 
    <option selected disabled>choose something</option> 
 
    <option>some text</option> 
 
    <option>some more text</option> 
 
    <option>and more and more</option> 
 
</select> 
 

 
<select> 
 
    <option selected disabled>choose something</option> 
 
    <option>some text</option> 
 
    <option>some more text</option> 
 
    <option>and more and more</option> 
 
</select> 
 

 
<select> 
 
    <option selected disabled>choose something</option> 
 
    <option>some text</option> 
 
    <option>some more text</option> 
 
    <option>and more and more</option> 
 
</select> 
 

 
<select> 
 
    <option selected disabled>choose something</option> 
 
    <option>some text</option> 
 
    <option>some more text</option> 
 
    <option>and more and more</option> 
 
</select> 
 

 
<button id="btn">Check</button>

+0

これは動作しません - イベントハンドラの中で 'var count = 0;' * '*を動かす必要があります。 – nnnnnn

+0

ありがとう@nnnnnn。それは私の側に愚かな間違いだった。私はコードを編集した –

関連する問題