简体   繁体   English

Javascript验证代码,以确保至少选中了一个复选框

[英]Javascript validation code to make sure that at least one checkbox is marked

How would I add javascript validation to make sure that at least one of the items is checked? 如何添加JavaScript验证,以确保至少检查了一项?

Select from courses below: 从以下课程中选择:

        <ul id="class_prices">
             <li>

                <input onclick="update()" name="lbsm" id="lsbm" type="checkbox" value="lbsm">
                <label>Law &amp; Business Study Material ($59.00)</label>
            </li>

            <li>

                <input onclick="update()" name="tsm" id="tsm" type="checkbox" value="tsm">
                <label>Trade Study Material ($59.00)</label>
            </li>
            <li>

                <input onclick="update()" name="lbepc" id="lbepc" type="checkbox" value="lbepc">
                <label>Law &amp; Business Exam Preperation Class ($50.00)</label>
            </li>
            <li>

                <input onclick="update()" name="tepc" id="tepc" type="checkbox" value="tepc">
                <label>Trade Exam Preperation Class ($50.00)</label>
            </li>

        </ul>

        </td>

            <td><h2>$<span id="coursetotal"></span></h2></td>
                </tr>

Here is javascript code that I have, which does not work. 这是我拥有的JavaScript代码,无法正常工作。

<script type="text/javascript">
  function valthisform() {
    var chkd = document.form1.lsbm.checked || document.form1.tsm.checked||  document.form1.lbepc.checked|| document.form1.tepc.checked

    if (chkd == true) { } else { alert ("please check a checkbox") }
  }
</script>

I will appreciate your feedback. 感谢您的反馈。

Get all the checkboxes inside the UL, and if any of them are checked, set the chkd variable to true : 获取UL内部的所有复选框,如果选中其中任何一个,请将chkd变量设置为true:

var inputs = document.getElementById('class_prices').getElementsByTagName('input'),
    chkd = false;

for (var i=inputs.length; i--;) {
    if (inputs[i].type.toLowerCase() == 'checkbox' && inputs[i].checked) chkd = true;
}

FIDDLE 小提琴

I just tested your javascript and it seems to be working as you expected. 我刚刚测试了您的JavaScript,它似乎按您的预期工作。 There is one problem with the script itself, and that is that it doesn't return a value. 脚本本身存在一个问题,那就是它不返回值。

<script type="text/javascript">
  function valthisform() {
    var chkd = document.form1.lsbm.checked || document.form1.tsm.checked||  document.form1.lbepc.checked|| document.form1.tepc.checked

    if (chkd) {
      return true; //Submit the form
    } else {
      alert ("please check a checkbox");
      return false; //Prevent it from being submitted
    }
  }
</script>

LiveValidationForm has a submit handler attached to the form too and that handler might interfere with your handler and/or prevent the form from being submitted. LiveValidationForm也有一个附加到表单的提交处理程序,该处理程序可能会干扰您的处理程序和/或阻止提交表单。


Fun fact: You have document.form1.lsbm.checked properly misspelled in your javascript, as the element in your document has a properly misspelled id of lsbm instead of lbsm too. 有趣的事实:你已经document.form1.lsbm.checked在JavaScript正确拼写错误,因为你的文档中的元素具有的正确拼写错误ID lsbm而不是lbsm了。

Try this code.. 试试这个代码。

HTML 的HTML

<ul>
    <li><label><input type="checkbox" value="id1" style="margin-right: 10px;" name="class" checked> ABC</label></li>
    <li><label><input type="checkbox" value="id2" style="margin-right: 10px;" name="class" checked>XYZ</label></li>
    <li><label><input type="checkbox" value="id3" style="margin-right: 10px;"name="class" checked> LMN</label></li>
</ul>

JavaScript: JavaScript:

<script>
    $('input[type="checkbox"][name="class"]').on('change', function () {
        var getArrVal = $('input[type="checkbox"][name="class"]:checked').map(function () {
        return this.value;
    }).toArray();
        if (getArrVal.length) {
        //execute the code
    } else {
        $(this).prop("checked", true);
        alert("Select at least one column");
        return false;
    }
    });
</script>

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

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