1

我试图查找并计算 div 内的所有输入字段,如果有任何按钮为空,则禁用按钮。但是有几个 div 具有相同的类。我只需要一个输入字段。尝试了十几件事,但它不起作用。它要么计算页面上的所有输入字段,要么不计算。

 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。目前,您正在找到所有具有类的 divshipdiv和下面的所有input字段。更新您的函数以调用.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 回答