0

ユーザーがフォームを送信するときに、多くの選択タグを含むフォームがあります。ユーザーがすべての選択タグに対して1つのオプションを選択したかどうかを確認したいのですが、これが私のjqueryコードです

$('#apForm select').each(function(){
            var $this = $(this);
            if ($this.selectedIndex == 0){
                var error = 'fill this please' ;
                $this.next('span').text(error);
                errorCount = errorCount + 1;   
            }
        });

そして私はこのように試しました

$this.attr("selectedIndex")

私のコードの一部を提供します。私の質問は、さらにコードを提供する必要があるかどうかを教えてください

ご協力ありがとう御座います

4

3 に答える 3

3
var $this = $(this);
if($this.get(0).selectedIndex == 0) {

}

または単に単純な

this.selectedIndex; // not $this / $(this)

いいえのoption場合は戻ります-1

ここで、上記のすべてのケースを示しました

于 2012-05-18T11:48:12.137 に答える
3

$this.selectedIndexではなく、this.selectedIndexです:)

  • これは HTMLDomElement です
  • $this は jQuery オブジェクトです
于 2012-05-18T11:52:59.750 に答える
0

1.6 より上のバージョンの jQuery を使用している場合は? その後、使用できます

if ( $this.prop('selectedindex') == 0 ){ /* handle */ }
于 2012-05-18T11:53:20.480 に答える