2016-09-25 74 views
1

我的代碼有問題,如果你幫我,我將不勝感激。問題是,當您正確填寫表單中的所有輸入時,腳本會從提交按鈕中刪除屬性「已禁用」,但例如,如果在填寫表單後清除所有字段,則提交按鈕將能夠提交表單,但它必須返回屬性「禁用」。我該如何解決它?我該如何解決我的表單驗證問題

//validation name 
     document.callbackform.name.onkeyup = function() { 
      var name = document.callbackform.name.value; 
      if (name === "") { 
       document.callbackform.name.removeAttribute("class", "ready"); 
       document.getElementById("callError").style.display = "block"; 
       document.getElementById("calllErrorTwo").style.display = "none"; 
      } else { 
        document.getElementById("callError").style.display = "none"; 
        var pattern = new RegExp("^[а-я]+$", "i"); 
        var isValid = this.value.search(pattern) >= 0; 
        if (!(isValid)) { 
         document.getElementById("calllErrorTwo").style.display = "block"; 
         document.callbackform.name.removeAttribute("class", "ready"); 
        } else { 
         document.getElementById("calllErrorTwo").style.display = "none"; 
         document.callbackform.name.setAttribute("class", "ready"); 
        } 
      } 
     }; 

     //validation phone 
     document.callbackform.phone.onkeyup = function() { 
      var name = document.callbackform.phone.value; 
      if (name === "") { 
       document.callbackform.phone.removeAttribute("class", "ready"); 
       document.getElementById("calltelError").style.display = "block"; 
       document.getElementById("calltelErrorTwo").style.display = "none"; 
      } else { 
        document.getElementById("calltelError").style.display = "none"; 
        var pattern = new RegExp("[- +()0-9]+"); 
        var isValid = this.value.search(pattern) >= 0; 

        if (!(isValid)) { 
         document.getElementById("calltelErrorTwo").style.display = "block"; 
        } else { 
         document.getElementById("calltelErrorTwo").style.display = "none"; 
         document.callbackform.phone.setAttribute("class", "ready"); 
        } 
       } 
     }; 

     //filling the form 
     document.callbackform.onkeyup = function() { 
      var a = document.callbackform.name.getAttribute("class"); 
      var c = document.callbackform.phone.getAttribute("class"); 
      if (a === "ready" && c === "ready") { 
       document.getElementById("subCallback").removeAttribute("disabled"); 
       document.getElementById("subCallback").style.cursor = "pointer"; 
      } else { 
       document.getElementById("subCallback").setAttribute("disabled"); 
       document.getElementById("subCallback").style.cursor = "not-allowed"; 
      } 
     }; 

回答

1

簡單修復。 .setAttribute("disabled");不起作用disabled是屬性,而不是屬性,因爲它沒有值。你只需要使用.disabled = true;如圖所示:

document.getElementById("subCallback").disabled = true; 

這也將是很好的使用以下方法來刪除禁用的屬性:

document.getElementById("subCallback").disabled = false; 

請記住,setAttribute()總是需要兩個參數,第二個參數是屬性值。