表中的复选框的Jquery验证代码

时间:2016-12-22 06:43:16

标签: javascript php jquery validation checkbox

Image of skills required 从上面的图像我添加了更多的功能, 所以我的问题是每个技能GD(或)PI(或)两个复选框都必须是强制性的。(必须选择至少一个复选框) 我怎么能实现这一目标。 这是我的HTML:

<a class="text-center btn btn-danger addSkills">+ Add Skills</a>
<input class="selectGdSkill" type="checkbox" count="0" id="skill[0][gdskill]" name="skill[0][gdskill]">
<input class="selectPiSkill" type="checkbox" count="0" id="skill[0][piskill]" name="skill[0][piskill]">

这是添加更多功能代码:

var skillcount = 1;
$(".addSkills").click(function () {
    $('#jobSkills tr:last').after('<tr>
    <td><input class="searchskill" count="' + skillcount + '" id="skill_' + skillcount + '_title" name="skill[' + skillcount + '][title]" type="text" autocomplete="off"></td><td><input count="' + skillcount + '" id="skill_' + skillcount + '_weightage" name="skill[' + skillcount + '][weightage]" type="text" autocomplete="off"></td>
    <td><select class="wp-form-control" name="skill[' + skillcount + '][type]"><option value="0">Select Test Type</option><option value="1">Practice Test</option><option value="2">Qualifying</option></select></td>
    <td><input  class="selectGdSkill" type="checkbox" count="' + skillcount + '" id="skill[' + skillcount + '][gdskill]" name="skill[' + skillcount + '][gdskill]"></td>
    <td> <input class="selectPiSkill" type="checkbox" count="' + skillcount + '" id="skill[' + skillcount + '][piskill]" name="skill[' + skillcount + '][piskill]"></td>
    <td><span class="removeSkill" id="' + skillcount + '" ><a style="color:red">Remove</a></span></td>
    </tr>');
    skillcount++;
});

帮助我小提琴:https://jsfiddle.net/r359b453/6/

3 个答案:

答案 0 :(得分:3)

在这里您的HTML我更改了输入框ID并放置了您的表格 <div id="myDiv"> <table id="tableID"> <tr> GD<input class="selectGdSkill" type="checkbox" id="selectGdSkill_0"> PI<input class="selectPiSkill" type="checkbox" id="selectPiSkill_1"> </tr> <br> <tr> GD<input class="selectGdSkill" type="checkbox" id="selectGdSkill_1"> PI<input class="selectPiSkill" type="checkbox" id="selectPiSkill_1"> </tr> </table> <button id="JobSubmit" class="btn btn-success text-center">SUBMIT JOB</button> </div>

这是用于检查的JavaScript代码。

    $(document).ready(function(){
    $("#JobSubmit").on("click",function(){          
        var IDs = [];
        $("#myDiv").find("input").each(function(){ IDs.push(this.id); });
         console.log(IDs);

         $.each(IDs, function(i, value) {
            if(!($("#"+value ).is(":checked"))){
                alert("Atleast one checkbox has to select from every tr");
                return false;
            }

         });

    })
})

答案 1 :(得分:2)

为所有复选框指定相同的班级名称并尝试使用此

Function answer(list As Range) As String

    Dim extent As Long
    Dim cell As Range

    extent = list.Rows.Count

    ReDim array_1(1 To extent)

    For Each cell In list.Cells
        Select Case cell.Value
            Case "L", "R", "PD", "D", "S"

            Case Else
                answer = "Your list is not valid"
                Exit Function

        End Select
    Next cell

    'Otherwise function will perform rest of code
    answer = "Your list is valid"

End Function

答案 2 :(得分:1)

如果每个技能的两个复选框都为空,请选中至少复选框

nonlocal

它适用于每个 var rows = document.getElementsByTagName('tr'); var isTableValid = true; for(var i=0;i<rows.length;i++) { var checkboxs=rows[i].getElementsByClassName("selectGdSkill");//add "selectPiSkill" class in this var okay=false; for(var j=0;j<checkboxs.length;j++){ console.log('here' + checkboxs[j].checked); if(checkboxs[j].checked){ okay=true; break; } } if(!okay && checkboxs.length > 0) { isTableValid = false; break; } } if(isTableValid) return true; else { alert("Please select atleast one checkbox every skill either GD or PI"); return false; } 但仅适用于<tr>

  

我正在寻找两个复选框GD(或)PI

中的任何人