空のフィールドがあるかどうかを確認するスクリプトを作成しています。空のフィールドがある場合は、それらを赤くして送信ボタンを無効にします。ただし、カウンターを使用して空のフィールドが見つかったかどうかを判断すると、各関数が停止します。
function submitDisable(){
$('.shipdiv input').each(function(){
if( $(this).val() == ''){
$(this).css("background-color","#ff0000");
$(this).closest('.shipdiv').find('#button-submit').attr('disabled', 'disabled');
counter++;
}
});
if(counter == "0"){
$(this).css("background-color","");
$(this).closest('.shipdiv').find('#button-submit').removeAttr('disabled');
}
}
jQuery(document).ready(submitDisable);
をコメントアウトするcounter++;
と、すべての空のフィールドがきれいに赤く色付けされます。そのままにしておくと、各ループが停止し、最初のフィールドのみが赤くなります。何故ですか?
ありがとう!