如何在JavaScript循环中提醒一次

1 javascript jquery loops

代码:在下面显示的代码中,警报消息一直循环直到语句结束.我需要一个警告语句只提醒一次.如何实现这一点?

这里是检查复选框的输出,如果未选中,则显示未定义

 for(j=1;j<=numOflimit;j++)
    {
      var select = $("input[name=select+j]:checked").val();
        //alert (select);
        //$check='undefined';


        if(select==undefined)

        {
            alert ("Please select atleast one product");
        }
        else
        {
            var postData    =   $("form").serialize();//"product_name="+product_name+"&barcode="+barcode+"&Quantity"+Quantity;
            $.ajax
            ({
               type: 'POST',
                url: 'http://localhost/example/index.php/castoutput',                      
                data: postData,
                success: function(html) {
                    // alert(html);
                    $('#cast2').html(html); 
                }
         });
        }
   }
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以创建一个true或false的变量,如果警报已经被触发,一旦将其置于false并检查if,如果是true或false.

if (showAlert==true)
{
   alert ("Please select atleast one product");
   showAlert = false;
}
Run Code Online (Sandbox Code Playgroud)