0

我有以下代码...

 var min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array

       for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    return false;
                }
            else
                {
                    $('.prompt accident').remove();
                                            sendAjaxRequest();
                }
        }

基本上,我通过将所需的元素存储在数组中来验证表单。然后我循环遍历数组并检查是否有任何空值。如果用户留下了一个值,则该元素旁边和该部分的选项卡上会显示一个星号。

一旦用户填写了所有标记的表单元素,代码就会删除星号并发送 Ajax 请求。

然而,一旦找到一个空元素,它就会显示星号并停止。

我需要的是它每次循环遍历整个数组并显示所有星号。我有一种感觉,这将非常简单,但无法找到任何东西或弄明白。

任何帮助,将不胜感激

4

3 回答 3

2
var hasError=false;
for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    hasError= true;
                    //return false; // remove this...
                } else $('.prompt accident').remove();
        }
  if(!hasError) {
       sendAjaxRequest();
  }
于 2012-10-30T13:33:24.643 回答
1

这里

将 return false 更改为noerror=false,添加var noerror=true到循环之前并以return noerror

像这样

var noerror=false, min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array
$('.prompt accident').remove(); // reset
for (var i=0;i<min_val.length;i++) { //loop through each element if array
  var object = min_val[i];
  if(object.value == "" || object.value == null) { //if element is null or empty then execute code
    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
    noerror=false;
  }
 }
 if (noerror) {              
  sendAjaxRequest();
 }
 return noerror;
}
于 2012-10-30T13:33:37.827 回答
0

不要return false。只是continue

编辑:更好的解决方案是:

 var min_val = [{"id":"driverDOA","value":date_of_accident},etc etc...];//put validation elements in an array
       var valid = true; // I MADE A CHANGE HERE
       for (var i=0;i<min_val.length;i++)//loop through each element if array
        {
            var object = min_val[i];
            if(object.value == "" || object.value == null)//if element is null or empty then execute code
                {
                    document.getElementById(object.id).parentNode.innerHTML += '<div class="prompt accident">*</div>';
                    document.getElementById('accident-tab').innerHTML += '<div class="prompt accident">*</div>';
                    valid = false; // AND HERE
                }
            else
                {
                    $('.prompt accident').remove();
                                            sendAjaxRequest();
                }
        }
        return valid; // AND HERE
于 2012-10-30T13:32:52.970 回答