简体   繁体   English

检查复选框是否已选中的问题

[英]Issue in checking whether checkbox is checked

I have written following function in jquery 我在jQuery中编写了以下功能

<script>
    function validateAppliesTo()
    {
        if ($("#collapseCat_row input:checkbox:checked").length > 0)
        {
            return true;
        } else
        {
            swal({
                title: "",
                text: "Please select any course for which the fee should apply! "
            });

            return false;
        }

        if ($("#accordion_cat input:checkbox:checked").length > 0)
        {
            return true;
        } else
        {
            swal({
                title: "",
                text: "Please select any category! "
            });

            return false;
        }

        return true;
    }
</script>

The above code works fine for #course_condition_row but does not works for #accordion_cat. 上面的代码适用于#course_condition_row,但不适用于#accordion_cat。

In html 在html中

<div class="panel panel-default">
    <div class="panel-heading">
        <h5 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion_cat" href="#collapseCat">Category</a>
        </h5>
    </div>
    <div id="collapseCat" class="panel-collapse collapse in">
        <div class="panel-body">
            <input name="student_category[]" class="cls_categories" id="General" value="3" type="checkbox">&nbsp;General<br>
            <input name="student_category[]" class="cls_categories" id="Reserved" value="4" type="checkbox">&nbsp;Reserved<br>
        </div>
    </div>

</div>

I want to validate the form. 我想验证表格。 If any checkbox is not selected in category accordian then it should return false... 如果未在Accordian类别中选中任何复选框,则它应返回false。

Please help me!!! 请帮我!!!

Once the validateAppliesTo() functions reaches a return , the rest of that function will not be excecuted. 一旦validateAppliesTo()函数到达return ,该函数的其余部分将不被执行。 Therefore, only the first if/else will be handled and the second will not be handled. 因此,将仅处理第一个if / else,而不会处理第二个。

In the code below I have removed the return 's and changed the if() statements. 在下面的代码中,我删除了return并更改了if()语句。

removing returns 去除退货

Removing the return 's will ensure that all the code is run. 删除return会确保所有代码都运行。

chaning if() statements 修改if()语句

If no checkbox is checked, .length will return 0, which in an if() statement is equal to true . 如果未选中任何复选框,则.length将返回0,在if()语句中该值等于true By adding the ! 通过添加! in front of the statement it will reverse this outcome. 在声明之前,它将扭转这一结果。 So in short: if no checked boxes are found, enter this if() statement. 简而言之:如果未找到复选框,请输入此if()语句。

I also changed #accordion_cat to #collapseCat so the JS matches the provide HTML. 我还将#accordion_cat更改为#collapseCat以便JS匹配提供的HTML。

result 结果

The result is that the swal() code is now called twice if no checkboxes are checked. 结果是,如果未选中任何复选框,则swal()代码现在将被调用两次。 I don't know what this function is supposed to do, but be aware that it is called twice, and that the second time might overwrite whatever result it had when it was called the first time. 我不知道该函数应该做什么,但是要知道它被调用了两次,并且第二次可能会覆盖第一次调用时的结果。

 $(function() { $( "#validate" ).click(function() { validateAppliesTo() }); function validateAppliesTo() { if (!$("#collapseCat_row input:checkbox:checked").length) { swal({ title: "", text: "Please select any course for which the fee should apply! " }); } if (!$("#collapseCat input:checkbox:checked").length) { swal({ title: "", text: "Please select any category! " }); } } function swal(obj){ console.debug('do swal with', obj); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel panel-default"> <div class="panel-heading"> <h5 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion_cat" href="#collapseCat">Category</a> </h5> </div> <div id="collapseCat" class="panel-collapse collapse in"> <div class="panel-body"> <input name="student_category[]" class="cls_categories" id="General" value="3" type="checkbox">&nbsp;General<br> <input name="student_category[]" class="cls_categories" id="Reserved" value="4" type="checkbox">&nbsp;Reserved<br> </div> </div> </div> <button id="validate">Validate</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM