1

div内のすべての入力フィールドを見つけて数え、空のボタンがある場合はボタンを無効にしようとしています。ただし、同じクラスの div がいくつかあります。入力フィールドは 1 つだけ必要です。ダースのことを試しましたが、うまくいきません。ページ上のすべての入力フィールドをカウントするか、まったくカウントしません。

 function submitDisable(){
    counter = 0;
    $('.shipdiv input').each(function(){
    if( $(this).val() == ''){
    $(this).css("background-color","#ff0000");
    $(this).closest('.shipdiv').find('#button-submit').prop('disabled', true);
    counter++;
}
    if( $(this).val() != ''){
    $(this).css("background-color","");
    counter-1;
}

    if(counter == "0"){
    $(this).closest('.shipdiv').find('#button-submit').prop('disabled', false);
    }
});

}

jQuery(document).ready(function(){
$('.shipdiv').click(submitDisable);
});

私は試しました:誰かが理解し、私を助けることができることを$(this, 'input')願っています。ありがとう!find();children();

4

2 に答える 2

2

検索範囲がクリックされた div に設定されていることを確認する必要があります。現時点では、クラスshipdivinputその下のすべてのフィールドを持つすべての div を見つけています。.find()クリックされた div で呼び出すように関数を更新します$(this)

function submitDisable(){
    counter = 0;
    $(this).find('input').each(function(){
        ..
    });
}
于 2013-07-11T07:56:03.250 に答える
0
if( $('.shipdiv input[value=""]').length>0 )
{
    $('.shipdiv input:first').closest('.shipdiv').find('#button-submit').prop('disabled', true);
}
于 2013-07-11T08:37:45.257 に答える