简体   繁体   English

如何确定复选框是否已选中

[英]How to determine if the checkbox has been checked or not

I am trying to create a validation for the borrow button that when pressed but there is no item that has not been check it supposed to show a prompt but what is happening is it will not show a prompt and also it will not save into the database obviously. 我正在尝试为借用按钮创建一个验证,该借用按钮在被按下但没有未被检查的项目时应该显示提示,但是正在发生的情况是它不会显示提示并且也不会保存到数据库中明显。 How would I try to do this. 我将如何尝试做到这一点。

Below is the code for javascript. 以下是javascript的代码。 (I don't know if this is even right) (我不知道这是否正确)

$(".uniform_on").change(function(){
    var max = 1;
    if ($(".uniform_on:checked").length < max) {
        $(".uniform_on").attr('disabled', 'disabled');
        alert('Please Check the item of equipment to be borrowed!');
        $(".uniform_on:checked").removeAttr('disabled');
    } else {
        $(".uniform_on").removeAttr('disabled');
    }
})

Please help I am not mostly aware of the code since I only customize the code on my own and I have a limited knowledge on this kind of stuff. 请帮助我并不是很了解代码,因为我只是自己定制代码,而且我对这类东西知之甚少。 What if I press Borrow Button without checking the checkbox; 如果我在不选中复选框的情况下按了“借用按钮”怎么办? how do I validate this that the user would check something first before pressing borrow again. 我该如何验证用户在再次按下借用之前先检查一下内容。 Thanks. 谢谢。

This is the whole code for my Borrow 这是我借的整个代码

<?php include('header.php'); ?>
<?php include('session.php'); ?>
<?php include('navbar_borrow.php'); ?>
    <div class="container">
        <div class="margin-top">
            <div class="row">   
                                <div class="alert alert-info">
                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    <strong><i class="icon-user icon-large"></i>&nbsp;Borrow Table</strong>
                                </div>

        <div class="span12">        

        <form method="post" action="borrow_save.php">
<div class="span3">

                                            <div class="control-group">
                <label class="control-label" for="inputEmail">Borrower Name</label>
                <div class="controls">
                <select name="member_id" class="chzn-select"required/>
                <option></option>
                <?php $result =  mysql_query("select * from member")or die(mysql_error()); 
                while ($row=mysql_fetch_array($result)){ ?>
                <option value="<?php echo $row['member_id']; ?>"><?php echo $row['firstname']." ".$row['lastname']; ?></option>
                <?php } ?>
                </select>
                </div>
            </div>
                <div class="control-group"> 
                    <label class="control-label" for="inputEmail">Due Date</label>
                    <div class="controls">
                    <input type="text"  class="w8em format-y-m-d highlight-days-67 range-low-today" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC;" required/>
                    </div>
                </div>
                <div class="control-group"> 
                    <div class="controls">

                                <button name="delete_student" class="btn btn-success"><i class="icon-plus-sign icon-large"></i> Borrow</button>

                    </div>
                </div>
                </div>
                <div class="span8">
                        <div class="alert alert-success"><strong>Select Equipment</strong></div>
                            <table cellpadding="0" cellspacing="0" border="0" class="table" id="example">

                                <thead>
                                    <tr>

                                        <th>Acc No.</th>
                                        <th>Equipment Description</th>                                 
                                        <th>Category</th>
                                        <th>Quantity</th>
                                        <th>status</th>
                                        <th>Add</th>

                                    </tr>
                                </thead>
                                <tbody>

                                  <?php  $user_query=mysql_query("select * from equipment where status != 'Archive' ")or die(mysql_error());
                                    while($row=mysql_fetch_array($user_query)){
                                    $id=$row['equipment_id'];  
                                    $cat_id=$row['category_id'];

                                            $cat_query = mysql_query("select * from category where category_id = '$cat_id'")or die(mysql_error());
                                            $cat_row = mysql_fetch_array($cat_query);
                                    ?>
                                    <tr class="del<?php echo $id ?>">


                                    <td><?php echo $row['equipment_id']; ?></td>
                                    <td><?php echo $row['equipment_description']; ?></td>
                                    <td><?php echo $cat_row ['classname']; ?> </td> 
                                    <td><?php echo $row['quantity']; ?> </td> 
                                    <?php /*  <td><?php echo $row['equipment_description']; ?></td> */ ?>
                                      <td width=""><?php echo $row['status']; ?></td> 
                                    <?php include('tooltip_edit_delete.php'); ?>
                                    <td width="20">
                                                <input id="" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>" >

                                    </td>

                                    </tr>
                                    <?php  }  ?>
                                </tbody>
                            </table>

                </form>
            </div>      
            </div>      
<script>        
$(".uniform_on").change(function(){
    var max = 1;
    if( $(".uniform_on:checked").length < max ){

        $(".uniform_on:checked").attr('disabled', 'disabled');
                 alert('Please Check the item of equipment to be borrowed!');
        $(".uniform_on:checked").removeAttr('disabled');

    }else{

         $(".uniform_on").removeAttr('disabled');
    }
})
</script>       
            </div>
        </div>
    </div>
<?php include('footer.php') ?>

Made changes and it work for the validation part now for the transaction part where I would want to borrow it won't save to the database but it prompted only that you have borrowed CODE new borrow button: 进行更改,它现在对于我想借用的交易部分的验证部分有效,它不会保存到数据库,但仅提示您借了CODE新的借用按钮:

<input  name="delete_student" class="btn btn-success" type="button" value="Borrow" onClick="return validationfunction();" />

CODE for the javascript: JavaScript代码:

<script>
function validationfunction() {
    if($(".uniform_on:checked").length > 0 ) {
         alert('Equipment has been borrowed.');
         return true
    }
    else {
         alert('Please check the item of equipment to be borrowed!');
         return false;
    }
}
</script>       

First of all its a javascript and jQuery code. 首先,它是一个javascript和jQuery代码。

Call this function on click of a bowrrow button to check if the checkbox is checked or not like below. 单击弓箭按钮以调用此函数,以检查复选框是否已选中,如下所示。

function validationfunction() {
    if($('.uniform_on').is(':checked')) {
         // Your code goes here.
    }
    else {
         alert('Please Check the item of equipment to be borrowed!');
         return false;
    }
}

Call this function on click of your button as follow. 单击按钮,调用此功能,如下所示。

<input type="submit" value="Borrow Button" onClick="return validationfunction();" />

As @SpYk3HH already mentioned that the part of you code you have posted here is not PHP. 正如@ SpYk3HH已经提到的,您在此处发布的代码部分不是PHP。 But as per reading your complete post. 但根据阅读您的完整文章。 I can guess that you are basically having some issue while receiving post data at Back-end (PHP) side. 我可以猜测,在后端(PHP)端接收发布数据时,您基本上遇到了一些问题。

Actually, HTML Element Checkbox are usually play role as BOOL values (true/false). 实际上, HTML元素复选框通常作为BOOL值(true / false)起作用。 So if the Checkbox is selected you will get the Checkbox Value in Post Data otherwise you won't receive it. 因此,如果选中了复选框,您将在过帐数据中获得复选框值,否则将不会收到它。

How to handle this situation: 如何处理这种情况:

Well, this is something about to play with your code, you should make an array before inserting your data in the Database: 好吧,这是您的代码所要玩的东西,您应该在将数据插入数据库之前创建一个数组:

$myData = array();
$myData['key1'] = $_POST['key1'];
$myData['key2'] = $_POST['key2'];
/*
    As you can see we are handeling if the Checkbox was not seleted and giving it the value '0' in this case.
*/
$myData['checkbox_key'] = !empty($_POST['checkbox_key']) ? $_POST['checkbox_key'] : 0;


// then insert your $myData array in to database or do anything you wants to do.

Furthermore, you can also validate your Checkbox before submitting the form: 此外,您还可以在提交表单之前验证您的Checkbox:

function validate_it(){
     if( $(".yourClassName:checked").length > 0 ){
         // Submit it or anything else
     }else{
         // alert or warn the user!
     }
}

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

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