如何使用javascript检查表是否存在相同的卷号

时间:2017-03-06 11:15:05

标签: javascript html

  1. 我们可以检查卷号是否已存在。

    使用javascript

  2. 我们可以验证这个

  3. 显示一条警告消息,表明存在卷号 已经在表格中

  4. 
    
    window.onload = function() {
           document.getElementById('new').style.display = 'none';
         };
         function addtable(){
    	   document.getElementById('new').style.display = 'block';
           Rollno = document.getElementById("roll_number");
           Name = document.getElementById("student_name");
           Class = document.getElementById("class");
           var Gender = null;
             var inputElements = document.getElementsByClassName('gender');
             for (var i = 0; inputElements[i]; ++i) {
                 if(inputElements[i].checked){
                  Gender = inputElements[i].value;
                  break;
                }
             };
             Age = document.getElementById("age");
             Phone = document.getElementById("phone_number");
             var Result = null; 
    	     var inputElements = document.getElementsByClassName('result');
    	     for(var i=0; inputElements[i]; ++i){
    	       if(inputElements[i].checked){
    	           Result = inputElements[i].value;
    	           break;
    	       }
    	     };
        var table = document.getElementById("new");
        rowCount = table.rows.length;
        row = table.insertRow(rowCount);
    
        row.insertCell(0).innerHTML= Rollno.value;
        row.insertCell(1).innerHTML= Name.value;
        row.insertCell(2).innerHTML= Class.value;
        row.insertCell(3).innerHTML= Gender;
        row.insertCell(4).innerHTML= Age.value;
        row.insertCell(5).innerHTML= Phone.value;
        row.insertCell(6).innerHTML= Result;
        row.insertCell(7).innerHTML='<input type="submit" value = "Delete" onclick="deleteRow(this)">';
    
       
        var roll = document.forms["student_detail"]["roll_number"].value;
        if (roll == "") {
            alert("Rollno must be filled out");
            return false;
        }
        var name = document.forms["student_detail"]["student_name"].value;
        if (name == ""){
            alert("Name must be filled out");
            return false;
        }
        var clas = document.forms["student_detail"]["class"].value;
        if (clas == "") {
            alert("select the class");
            return false;
        }
        var age = document.forms["student_detail"]["age"].value;
        if (age == ""){
            alert("Age must be filled out");
            return false;
        }
        var phone = document.forms["student_detail"]["phone_number"].value;
        if (phone == "") {
            alert("Phone number must be filled out");
            return false;
        }
        if (document.student_detail.result1.checked == true && document.student_detail.result2.checked == true){
            alert('Select any one result');
            return false ;
         }
    
        }
    
        function deleteRow(obj) {
          
        var index = obj.parentNode.parentNode.rowIndex;
        var table = document.getElementById("new");
        table.deleteRow(index);
        
        }
        function myFunction() {
          var x = document.getElementById('myTable');
          if (x.style.display === 'none') {
            x.style.display = 'block';
          } else {
            x.style.display = 'none';
          }
          var change = document.getElementById("toggle");
          if (change.innerHTML === "Hide Form")
          {
            change.innerHTML = "Show Form";
          }
          else {
            change.innerHTML = "Hide Form";
          }
        }
        function hideElem(){
        document.getElementById('new').style.visibility = "hidden";
        }
        function showElem(){
        document.getElementById('new').style.visibility = "visible";
        }
    &#13;
    .abc table{
        	 width: 100%;
        	 border-collapse: collapse;
        }
        .abc table th{
            border: 1px solid #000;
        }
        .abc table td{
            border: 1px solid #000;
        }
        h2{
        color: black;
        text-shadow: 2px 2px 8px #FF0000
        }    
        input[type=text],select,input[type=number]{
        width: 100%;
        padding: 12px 20px;
        margin: 8px 0;
        display: inline-block;
        border: 2px solid #ccc;
        border-radius: 4px;
        box-sizing: border-box;
        }
        input[type=button] {
        width: 50%;
        background-color: #4CAF50;
        color: white;
        padding: 14px 10px;
        margin: 8px 0;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        }
    
        input[type=button]:hover {
        background-color: #45a049;
        }
    
        div {
         border-radius: 5px;
         background-color: #f2f2f2;
         padding: 20px;
        }
    &#13;
     
         
        
        <form name="student_detail" method="post" action="#" onsubmit="return addtable();">
        <table id="myTable" >
            <tr>
                <td><h2>School Management Application</h2></td>
            </tr>
            <tr>
                <td><label for="roll_number">Roll no</label></td>
                <td><input type="text" id="roll_number" name="roll_number" placeholder="Roll Number"></td>
            </tr>
            <tr>
                <td><label for="student_name">Student name</label></td>
                <td><input type="text" id="student_name" name="student_name" placeholder="Student Name"></td>
            </tr>
            <tr>
                <td><label for="class">Class</label></td>
                <td><select name="class" id="class">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                </select></td>
            </tr>
            <tr>
                <td><label>Gender</label></td>
                <td><input type="radio" class="gender" name="gender" value="male">Male
                <input type="radio" class="gender" name="gender" value="female">Female</td>
            </tr>
            <tr>
                <td><label for="age">Age</label></td>
                <td><input type="number" id="age" name="age" placeholder="Age"></td>
            </tr>
            <tr>
                <td><label for="phone_number">Phone number</label></td>
                <td><input type="text" id="phone_number" name="phone_number" placeholder="Phone Number"></td>
            </tr>
            <tr>
                <td><label>Result</label></td>
                <td><input type="checkbox" class="result" name="result1" value="passed" >Passed
                <input type="checkbox" class="result" name="result2" value="failed" />Failed</td>
            </tr>
            <tr>
                <td><input type="button" value="Submit" onclick="addtable()"></td>
            </tr>
        </table>
        </form>
         <table>
            <tr>
            <td><input type="button" value="Hide Form" id="toggle" onclick="myFunction()">
                <input type="button" value="Hide table" id="tab" onclick="hideElem()">
                <input type="button" value="Show table" id="tab1" onclick="showElem()"></td>
            </tr>
         </table>
    
        <div class="abc">
        	<table id="new">
              <tr>
              <th>Rollno</th>
        	  <th>Student name</th>
        	  <th>Class</th>
        	  <th>Gender</th>
        	  <th>Age</th>
        	  <th>Phone number</th> 
        	  <th>Result</th>
        	  </tr>
            </table>
        </div>
       
    &#13;
    &#13;
    &#13;

    • 如果再次输入相同的卷号,则需要提醒。之后 提交和显示警告信息

      任何人都可以帮我这样做

1 个答案:

答案 0 :(得分:0)

这是一个检查重复的函数

checkDupes($("#someId"));


function checkDupes(itemArray) {
            var isdupe = false;
            for (var i = itemArray.length - 1; i >= 0; i--) {
                var value = itemArray[i].value;
                if (value == null || value == '' || value.trim().length == 0) {
                    itemArray[i].style.backgroundColor = 'red';
                } else {
                    if (i > 0) {
                        for (var j = i - 1; j > -1 && i > 0; j--) {
                            if (value.trim().toLowerCase() == itemArray[j].value.trim().toLowerCase()) {
                                itemArray[i].style.backgroundColor = 'red';
                                isdupe = true;
                            }
                        }
                        if (!isdupe) {
                            itemArray[i].style.backgroundColor = 'transparent';
                        }
                    }
                }
            }
        }