我如何在JavaScript中使用数组时迭代第二个for循环

时间:2014-07-08 05:48:05

标签: javascript loops for-loop nested-loops

I have array which  length is 13 .
form has 13 simultaneous  fields user can enter in any field i want that if user  enter   5 or 7 or any field value then i want add validation that it's previous field  value should not be empty and it should not check validation for next field.

我使用过此代码......

datesId[0] = "bankNocForTorDateId";

datesId[1] = "advertisingDateShortlistingId";
datesId[2] = "torShortlistFinalizedDateId";
datesId[3] = "bankNocForShortlistDateId";
datesId[4] = "rfpDraftToBankDateId";
datesId[5] = "bankNocForRfpDateId";
datesId[6] = "rfpIssuedDateId";
datesId[7] = "proposalReciptDateTechnicalId";
datesId[8] = "evaluationFinalTechnicalDateId";
datesId[9] = "bankNocTechnicalDateId";
datesId[10] = "proposalReciptDateFinancialId";
datesId[11] = "evaluationFinalCombinedDateId";
datesId[12] = "nocBankDraftDate";



for(var i = 0; i<datesId.length ; i++ ){
  if(!(document.getElementById (datesId[i]).value == "")){
    for(var j =datesId[i].length-1 ;j>0 ; j-- ){
      if(document.getElementById(datesId[j]).value == ""){
        var message = "Please Enter "+datesLabel[j];
        alert(message);
        return false;
      }
    }
  }
}

实际上我是Javascript中的新手...对它没有太多的了解。我已经在java的基础上制作了这个逻辑。请清楚说明基本的区别。

先谢谢

1 个答案:

答案 0 :(得分:0)

完成!

var datesId = []
datesId[0] = "bankNocForTorDateId";
datesId[1] = "advertisingDateShortlistingId";
datesId[2] = "torShortlistFinalizedDateId";
datesId[3] = "bankNocForShortlistDateId";
datesId[4] = "rfpDraftToBankDateId";
datesId[5] = "bankNocForRfpDateId";
datesId[6] = "rfpIssuedDateId";
datesId[7] = "proposalReciptDateTechnicalId";
datesId[8] = "evaluationFinalTechnicalDateId";
datesId[9] = "bankNocTechnicalDateId";
datesId[10] = "proposalReciptDateFinancialId";
datesId[11] = "evaluationFinalCombinedDateId";
datesId[12] = "nocBankDraftDate";

// if datesId[6] value is not blank
if (datesId[6] != ''){
  // validate previous 5 field values should not be blank, but it should not check next 6 value.
  for (var i = (6-5); i < 6; i ++){
    console.log('validating field ' + datesId[i] + " (index=" + i + ")")
    if (datesId[i] == ''){
      alert('Field ' + datesId[i] + " must not be blank!")
    }
  }
}