2017-04-16 65 views
0

我有一個表單,其中加載頁面時提交按鈕被禁用。當類已被刪除時啓用表單提交按鈕

window.onload=function() 
{ 
    document.getElementById('form_submit').disabled = true; 
} 

當是空的或不正確的字段中輸入以下類添加到輸入字段:

if(document.getElementById('thickness').value == false) 
{ 
    document.getElementById('div_thickness').className = "form-group has-warning"; 
} 
else if(document.getElementById('thickness').value > 1 && document.getElementById('thickness').value < 7) 
{ 
    document.getElementById('div_thickness').className = "form-group has-success"; 
} 
else 
{ 
    document.getElementById('div_thickness').className = "form-group has-error"; 
} 

當所有錯誤或警告類已被刪除提交應再次啓用,但該按鈕住宿禁用。我缺少任何衝突嗎?

if(document.getElementsByClassName('has-error') || document.getElementsByClassName('has-warning')) 
{ 
    document.getElementById('form_submit').disabled = true; 
} 
else 
{ 
    document.getElementById('form_submit').disabled = false; 
} 

回答

1

當您查詢元素時,會返回一個空數組,因此會有一些內容。您需要檢查數組長度:

if(document.getElementsByClassName('has-error').length > 0 || document.getElementsByClassName('has-warning').length > 0) 
+0

感謝您的提示! – Muiter

+0

不客氣:-) –

0

else if(document.getElementById('thickness').value > 1 && document.getElementById('thickness').value < 7) 
 
    { 
 
     document.getElementById('div_thickness').className = "form-group has-success"; 
 
    if(document.getElementById("#div_thikness").classList.contain('form-group has-error'){ 
 
    $document.getElementById('#div_thickness).classList.remove('has-error); 
 
    } 
 
if(document.getElementById("#div_thikness").classList.contain('form-group has-warning'){ 
 
    $document.getElementById('#div_thickness).classList.remove('has-warning); 
 
    } 
 
    }​

更新時,您必須刪除成功有預警,有錯誤類不僅添加有佳績上面的代碼。

相關問題