如何查看复选框未选中

时间:2012-01-16 02:45:15

标签: php html forms function checkbox

如果用户没有选中任何复选框,我想提醒用户,然后返回表单 在他们检查了至少一个复选框后,进入下一页 我怎么能这样做?

<input type="checkbox" name="pay[]"  value="
    <?php 
    echo $DateArr[$record_count].";";
    echo $invoiceArr[$record_count].";";
    echo $TotalArr[$record_count].";";
    echo $amount_dueArr[$record_count];
    ?>
    " onClick="checkTotal()"/>

<td ALIGN="right" ><input type="submit" name="submit" value="Continue" onMouseOver="CheckBoxStatus()"></td>

javascript:

function CheckBoxStatus()
                      {

                        var checkbox_status = document.getElementsByName("pay[]").value;                            
                            if (checkbox_status = ";;;")
                            {
                               alert("Please select at least one to proceed!");
                               return false;
                            }
                            else
                            {
                              alert("Continue!");
                              return true;
                            }


                      }

如果用户未选中,则至少选中一个复选框,提醒消息并返回表单

然后

<form name="payform"  method="POST" action="payment.php"  onSubmit=" CheckBoxStatus()">

如果用户没有选中任何复选框,我该如何留在表格中?

可能是这样的:

function CheckBoxStatus()
                          {
                                if (document.getElementsByName("pay[]").checked == true) 
                                {
                                    document.forms["payform"].submit(); 
                                    return true;
                                }
                                else
                                {
                                    alert("Please select at least one invoice to proceed!");
                                   document.forms["payform"].reset();   
                                   return false;

                                }
                          }
我可以这样做: `

function CheckBoxStatus()
                          {
                            var status=$("pay[]").attr('checked');

                            if (status=true)
                            {
                             //if(document.getElementsByName("pay[]").checked) {
                                return true;
                            }
                            else {
                                alert("Please select at least one to proceed!");
                                return false;
                            }


                          }`

4 个答案:

答案 0 :(得分:0)

使用jQuery

var $checked = $("input:checked");
if($checked.length == 0) {
     alert("Please select at least one to proceed!");
}

答案 1 :(得分:0)

HTML有点混乱。有几件事......

  1. 删除包含该按钮的<td>标记。它看起来没有目的。
  2. 要调用JavaScript,您需要使用onsubmit而不是onMouseOver
  3. 要检查是否选中了复选框,请将您的功能更改为以下内容。

    function checkBoxStatus { 
        if(document.getElementsByName("pay[]").checked) {
            return true;
        }
        else {
            alert("Please select at least one to proceed!");
            return false;
        }
    }
    
  4. EDIT;忘了提醒

答案 2 :(得分:0)

  1. 检查是否包含了jquery。 :|
  2. 试试这个:

    <input id="ipay" type="checkbox" name="pay[]"  value="
    <?php 
    echo $DateArr[$record_count].";";
    echo $invoiceArr[$record_count].";";
    echo $TotalArr[$record_count].";";
    echo $amount_dueArr[$record_count];
    ?>"
    />
    
    <td ALIGN="right" ><input type="submit" name="submit" value="Continue"></td>
    <form id="pform" name="payform"  method="POST" action="payment.php">
    
  3. 使用Javascript:

        $(document).ready(function() {
                $('#pform').submit(function(e) {
                        var paycheck = $('#ipay').attr('checked');
                        if(paycheck != true or paycheck != 'checked') {
                                alert('Please check at least one option.');
                                e.preventDefault();
                        }
                });
        });
    

答案 3 :(得分:0)

您可以这样做:

<html>
    <head>
        <title>Verify Checkbox</title>
        <script type="text/javascript">
            function verifyCheckboxSelected(form) {
                var theForm = document.forms[form];
                var valid = false;

                for( var i=0; i < theForm.length; i++ ) { 
                    if(theForm.elements[i].checked) {
                        valid = true;
                    } 
                }

                if (!valid) {
                    alert('You must select at least one checkbox')
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <form name="myForm" onSubmit="return verifyCheckboxSelected(this.name)">
            <input type="checkbox" name="myCheckBoxes[]"/>Checkbox A</br>
            <input type="checkbox" name="myCheckBoxes[]"/>Checkbox B</br>
            <input type="checkbox" name="myCheckBoxes[]"/>Checkbox C</br>
            <input type="submit" value="submit" />
        </form>
    </body>
</html>