防止jquery追加上的重复行

时间:2014-02-28 08:13:09

标签: javascript php jquery

我有这个js用于追加字段,检查空白字段,以及防止行重复值。但它仅适用于检查空白字段。此代码在展示位置或其他方面是否存在任何不匹配?感谢。

   <script>
    count4 = 1;
    function appendCertification(){
        var certification = $('#certification').val(); 
        var skillSector = $('#skillSector').val();
        var issueDate = $('#issueDate').val();
        var expirDdate = $('#expiryDate').val();

        if(cerfication == "" || skillSector == "" || issueDate == "" || expiryDate == "") {
             alert("please fill minimum 1 row");
             return false;
         }
         for (var i = 0, row; row = document.getElementById("#certificationTable").rows[i]; i++) {
            var fields = new Array();
            for (var j = 0, col; col = row.cells[j]; j++) {
                fields[j] = col.innerHTML;
            }
            if(certification == fields[0] && skillSector == fields[1] && issuedate == fields[2] && expireddate == fields[3]) {
                alert("Duplicate row");
                return false;
                }
         }
        var field = "<tr><td>"+certification+"</td><td>"+skillSector+"</td><td>"+issueDate+"</td><td>"+expiryDate+"</td><input type='hidden' name='certificationVal[]' value='"+certification+"'><input type='hidden' name='sectorSkillVal[]' value='"+sectorSkill+"'><input type='hidden' name='issueDateVal[]' value='"+issueDate+"'><input type='hidden' name='expiryDateVal[]' value='"+expiryDate+"'></tr>";
        $("#certificationTable tbody").append(field);
        count++;
    };

    function clearform(){
        $("#certificationTable tbody").html("");
    };
  </script>

1 个答案:

答案 0 :(得分:0)

没有考虑变量名称的错误输入,document.getElementById使用没有#

的元素的id

所以错误就在这一行

for (var i = 0, row; row = document.getElementById("#certificationTable").rows[i]; i++) {

应该是

for (var i = 0, row; row = document.getElementById("certificationTable").rows[i]; i++) {