如何在jquery中调用jquery动态id和sum验证

时间:2016-09-28 07:41:40

标签: javascript php jquery validation

点击+添加技能按钮,我可以添加更多技能 如何通过查询验证总和为100。

我想知道如何调用技能和重量的id ..? enter image description here

我的表单代码是这样的:

<div class="col-lg-12">
    <h4>Skills Required</h4>
    <div class="col-md-10">
        <table id="jobSkills" class="col-lg-10" style="border:1px;">
            <tbody>
                <tr>
                    <td class="col-xs-4">Skill</td>
                    <td class="col-xs-4">Weightage</td>
                    <td class="col-xs-4">Test Type</td>
                    <td class="col-xs-4">Assign to GD-Skill</td>
                    <td class="col-xs-4">Assign to PI-Skill</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="col-md-2">
        <span class="text-center btn btn-danger addSkills">+ Add Skills</span>
    </div>
</div>

这是我的addmore脚本

var skillcount=0;

$(".addSkills").click(function(){
    $('#jobSkills tr:last').after('<tr>
    <td class="col-xs-4"><input class="wp-form-control searchskill" count="' + skillcount + '" name="skill[' + skillcount + '][title]" type="text" autocomplete="off"></td>
    <td class="col-xs-4"><input class="wp-form-control" count="' + skillcount + '" name="skill[' + skillcount + '][weightage]" type="text" autocomplete="off"></td>
    <td class="col-xs-2"><span class="removeSkill" id="' + skillcount + '" ><a style="color:red">Remove</a></span></td>
    </tr>');
    skillcount++;
    //console.log(skillcount);
});

$("#jobSkills").on('click','.removeSkill',function(){
    console.log($(this).parent());
    $(this).parent().parent().remove();
});

1 个答案:

答案 0 :(得分:-2)

我得到了总和的解决方案:

var sum = 0;
for(var i = 0; i < skillcount; i++){
    sum += parseFloat(document.getElementsByName('skill[' + i + '][weightage]')[0].value);
}
if( sum == NaN || sum != 100){
    alert('Sum of All skill weightage must be 100');
    return false;
}