Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery计数器停止每个函数_Jquery_Each - Fatal编程技术网

jquery计数器停止每个函数

jquery计数器停止每个函数,jquery,each,Jquery,Each,我正在制作一个脚本,检查是否有空字段,如果有,则使其变为红色并禁用提交按钮。但是,在使用计数器确定是否找到任何空字段时,这会停止每个函数 function submitDisable(){ $('.shipdiv input').each(function(){ if( $(this).val() == ''){ $(this).css("background-color","#ff0000"); $(this).closest('.shipdiv').f

我正在制作一个脚本,检查是否有空字段,如果有,则使其变为红色并禁用提交按钮。但是,在使用计数器确定是否找到任何空字段时,这会停止每个函数

    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);
注释掉
计数器++
和所有空字段都被很好地涂成红色,保留它,它会停止每个循环,只有第一个字段是红色的。为什么呢


谢谢

您可能没有创建变量“计数器”?添加
var计数器=0在函数前面:)

计数器是全局计数器吗?如果是这样,则最初应将其设置为
0

第二个观察:您的比较应该是
计数器===0
,而不是
计数器==“0”


另外,
submitDisable()
只运行一次(在document ready事件中)。也许您希望在编辑字段后运行它。在这种情况下,
counter
应该是
submitDisable()

在哪里定义了
counter
计数器?计数器是全局变量?顺便说一句,我真的不明白你的逻辑,你不应该在提交表单上称为submitDisable吗?请检查您的控制台是否有错误。您可以发布HTML吗?也许在JSFIDLE中发布您的代码?我在哪里可以检查我的控制台是否有错误?我发现是的,我习惯于php,在那里您可以动态定义变量。