使用javascript或jquery对表中动态生成的行进行验证

时间:2014-03-08 07:10:26

标签: jquery

在此上下文中,动态生成的行未在行级验证,并且在追加行之前也存在的起始行在单击提交按钮时不会同时验证。

    $("#addrow").click(function(newid){
            var table = document.getElementById("dataTable");

            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var element1 = document.createElement("select");

            var option1 = document.createElement("option");
            option1.innerHTML = "--Select Project--";
            option1.value = "";

            element1.add(option1);
            var option2 = document.createElement("option");
            option2.innerHTML = "Option2";
            option2.value = "2";
            element1.add(option2, null);

            element1.id="project"+(rowCount+1);
            element1.style.width="225px";
            cell2.appendChild(element1);

            var cell3 = row.insertCell(2);
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "--Select Module--";
            option1.value = "";

            element2.add(option1);
            element2.id="module"+(rowCount+2);
            var option2 = document.createElement("option");
            option2.innerHTML = "Option2";
            option2.value = "2";
            element2.add(option2, null);
            cell3.appendChild(element2);

            var cell4 = row.insertCell(3);
            var element3 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "--Select Sprint--";
            option1.value = "";

            element3.add(option1);
            element3.id="sprint"+(rowCount+2);
            var option2 = document.createElement("option");
            option2.innerHTML = "Option2";
            option2.value = "2";
            element3.add(option2, null);
            cell4.appendChild(element3);

            var cell5= row.insertCell(4);
            var element4 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "--Select Category--";
            option1.value = "";

            element4.add(option1);
            element4.id="category"+(rowCount+3);
            var option2 = document.createElement("option");
            option2.innerHTML = "Option2";
            option2.value = "2";
            element4.add(option2, null );

            cell5.appendChild(element4);

            var cell6 = row.insertCell(5);
            var element5 = document.createElement("input");
            element5.type = "text";
            element5.name = "txtbox[]";
            element5.className="spent";
            element5.style.width="50px";
            element5.style.height="15px";

            cell6.appendChild(element5);

            var cell7= row.insertCell(6);
            var element6 = document.createElement("input");
            element6.type = "text";
            element6.name = "txtbox[]";
            element6.className="remain";
            element6.style.width="50px";
            element6.style.height="15px";
            cell7.appendChild(element6);
            $(".spent").spinner({ min: 1, max: 12 });
            $(".remain").spinner({ min: 0, max: 50 });

            var cell8 = row.insertCell(7);
            var element7 = document.createElement("textarea");
            element7.name = "txtbox[]";
            element7.id="comments"+(rowCount+4);
            element7.style.width="95px";
            element7.style.height="30px";

            cell8.appendChild(element7);

            $("#validate").click(function(){
                    var rowcount=table.rows.length;
                    for(var i=1;i<rowcount;){
                            for(var j=1;j<=5;j++){
                                    var project=element1.id;
                                    var module=element2.id;
                                    var sprint=element3.id;
                                    var category=element4.id;
                                    var comments=element7.id;

                                    var prjt=$("#"+project+" option:selected").text();
                                    if(prjt=="--Select Project--"){

                                            alert("please select the project");

                                            return false;

                                    } else{
                                            $("#"+project).css({"border":"#ffffff"});
                                    }

                                    var mdule=$("#"+module+" option:selected").text();
                                    if(mdule=="--Select Module--"){
                                            alert("please select the Module");
                                            $("#"+module).css({"border":"1px solid red"});
                                            return false;

                                    } else{
                                            $("#"+module).css({"border":"#ffffff"});
                                    }

                                    var sprnt=$("#"+sprint+" option:selected").text();
                                    if(sprnt=="--Select Sprint--"){
                                            alert("please select the Sprint");
                                            $("#"+sprint).css({"border":"1px solid red"});
                                            return false;

                                    } else{
                                            $("#"+sprint).css({"border":"#ffffff"});
                                    }

                                    var catgory=$("#"+category+" option:selected").text();
                                    if(catgory=="--Select Category--"){
                                            alert("please select the Category");
                                            $("#"+category).css({"border":"1px solid red"});
                                            return false;

                                    } else{
                                            $("#"+category).css({"border":"#ffffff"});
                                    }

                                    var coments=$("#"+comments).val();
                                    if(coments == ""){
                                            alert("please enter comments");
                                            $("#"+comments).css({"border":"1px solid red"});
                                            return false;

                                    } else{
                                            $("#"+comments).css({"border":"#ffffff"});
                                    }
                                    i++; 
                            }
                    }
            });

    return newid;
});

    $("#validate").click(function(){
            function valid(){
                    var projectSearch_projectName=$( ".projectName option:selected" ).text();

                    if (projectSearch_projectName == "--Select Project--") {
                            alert("please select one option");
                            $(".projectName").css({"border":"1px solid red"});
                            return false;

                    } else{
                            $(".projectName").css({"border":"#ffffff"});


                    }

                    var module=$( ".module option:selected" ).text();
                    if (module == "--Select Module--") {

                            alert("please select one option");
                            $(".module").css({"border":"1px solid red"});
                            return false;

                    } else{
                            $(".module").css({"border":"#ffffff"});


                    }

                    var sprints=$( ".sprints option:selected" ).text();
                    if (sprints == "--Select Sprint--") {

                            alert("please select one option");
                            $(".sprints").css({"border":"1px solid red"});
                            return false;

                    } else{
                            $(".sprints").css({"border":"#ffffff"});


                    }
                    var category=$( ".category option:selected" ).text();
                    if (category == "--Select Category--") {

                            alert("please select one option");
                            $(".category").css({"border":"1px solid red"});
                            return false;

                    } else{
                            $(".category").css({"border":"#ffffff"});


                    }

                    var comments=$(".comments").val();
                    if (comments == "") {
                            alert("Please enter the Comments");
                            $(".comments").css({"border":"1px solid red"});
                            return false;

                    }

                    else 
                    {
                            $(".comments").css({"border":"ffffff"});
                    }
            } 

    });
});

1 个答案:

答案 0 :(得分:0)

此代码调用$("#validate").click(function() {,然后调用$("#validate").click(function(){。第二个调用不是添加到单击#validate时完成的事情,而是覆盖第一个。

可以将它们转换为命名函数,然后调用:

$("#validate").click(function() {
    firstFunction();
    secondFunction();
});

如果你想将这两个功能分开。