2011-08-09 539 views
3

我有一個for循環,通過class="required"通過每個輸入,它的空它改變了它的背景色。此外,它還向頁面的一部分添加了一條消息:「需要紅色背景色的字段」。但是,我只希望消息顯示一次。即使5個字段爲空,也只應顯示一條消息。有什麼建議麼?只運行一次?

繼承人我的代碼:

<script type="text/javascript" > 
window.onload = function() { 
document.getElementById("formID").onsubmit = function() { 
    var fields = this.getElementsByClassName("validate"), 
     sendForm = true; 
    for(var i = 0; i < fields.length; i++) { 
     if(!fields[i].value) { 
      fields[i].style.backgroundColor = "#ffb8b8"; 

    var inst = document.getElementById('inst'); 
     var reason = inst.insertRow(-1); 
     var cell1=reason.insertCell(0); 
     cell1.innerHTML = '<p>Fields with a red background are required and empty. Please fill them in and try resubmitting. Thanks.</p>'; 
     inst.appendChild(reason); 


    sendForm = false; 
     } 

    else { 
      fields[i].style.backgroundColor = "#fff"; 
     } 
} 
    if(!sendForm) { 
     return false; 
    } 
} 
} 
</script> 
+0

是jQuery的一個選項? – asawyer

+0

@Asawyer不,對不起... –

回答

2

這應該是相當簡單 - 你已經有了一個布爾變量來說明驗證是否失敗 - sendForm。因此,只需將「append message」部分從循環中移出並放入if。

document.getElementById("formID").onsubmit = function() { 
    var fields = this.getElementsByClassName("validate"), 
     sendForm = true; 
    for(var i = 0; i < fields.length; i++) { 
     if(!fields[i].value) { 
      fields[i].style.backgroundColor = "#ffb8b8"; 

      sendForm = false; 
     } 

    else { 
      fields[i].style.backgroundColor = "#fff"; 
     } 
    } 
    if(!sendForm) { 
     var inst = document.getElementById('inst'); 
     var reason = inst.insertRow(-1); 
     var cell1=reason.insertCell(0); 
     cell1.innerHTML = '<p>Fields with a red background are required and empty. Please fill them in and try resubmitting. Thanks.</p>'; 
     inst.appendChild(reason); 
     return false;    
    } 
} 
+0

當我這樣做,它說'未終止的字符串文字'指向'''之前'' –

+0

@Jonah - 適用於我:http://jsfiddle.net/QSXGC/ – Jamiec

3

可能您被設置爲true任何時間的字段驗證失敗的標誌。循環執行後,如果該標誌爲真,則附加錯誤消息。

0

添加一個布爾標誌,以表明您已經顯示的消息

window.onload = function() { 
    var notified = false; 

    for(var i = 0; i < fields.length; i++) { 
     if(!fields[i].value) { 
      fields[i].style.backgroundColor = "#ffb8b8"; 
      ... 
      if (!notified) { 
       cell1.innerHTML = '<p>Fields with a red background are required and empty. Please fill them in and try resubmitting. Thanks.</p>'; 
       inst.appendChild(reason); 
       notified = true; 
      } 
      .... 
} 
+0

這給了我一個錯誤'缺少變量名稱'引用'var notifications..' –

1

使用標誌初始化爲假,一旦u得到任何的空場,使其真實,只打印如果消息國旗是錯誤的。這裏

<script type="text/javascript" > 
window.onload = function() { 
document.getElementById("formID").onsubmit = function() { 
    var fields = this.getElementsByClassName("validate"), 
     sendForm = true; 
    var flag=false; 
    for(var i = 0; i < fields.length; i++) { 
     if(!fields[i].value) { 
      fields[i].style.backgroundColor = "#ffb8b8"; 
    if(flag==false) { 
     var inst = document.getElementById('inst'); 
     var reason = inst.insertRow(-1); 
     var cell1=reason.insertCell(0); 
     cell1.innerHTML = '<p>Fields with a red background are required and empty. Please fill them in and try resubmitting. Thanks.</p>'; 
     inst.appendChild(reason); 
    } 
    flag=true; 


    sendForm = false; 
     } 

    else { 
      fields[i].style.backgroundColor = "#fff"; 
     } 
} 
    if(!sendForm) { 
     return false; 
    } 
} 
} 
</script> 
+0

工作好!謝謝.. –