Check all elements in form with Javascript

前端 未结 4 1034
庸人自扰
庸人自扰 2021-02-04 20:41

I know javascript in the beginning level, but I have a problem.

I have 7 input elements in a form and I want all of them to be filled. I came up with t

4条回答
  •  伪装坚强ぢ
    2021-02-04 21:13

    You could just do this:

    //Declare variables
    var 1, 2, 3, 4, 5, 6, 7;
    1 = document.getElementById("Field Id");
    2 = document.getElementById("Field Id");
    3 = document.getElementById("Field Id");
    4 = document.getElementById("Field Id");     //Define variable values
    5 = document.getElementById("Field Id");
    6 = document.getElementById("Field Id");
    7 = document.getElementById("Field Id");
    
    //Check if any of the fields are empty
    If (1 == "" || 2 == "" || 3 == "" || 4 == "" || 5 == "" || 6 == "" || 7 == "") {
      alert("One or more fields are empty!");
      //Other code
    }
    

    I used this for my own form and it works fine while not taking up to much space or looking too "ugly". It works for all field elements and checks the value entered.

提交回复
热议问题