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

时间:2016-04-06 12:00:48

标签: javascript jquery

我正在尝试创建一个借用按钮的验证,当按下但是没有项目没有检查它应该显示提示但发生的事情是它不会显示提示而且它也不会保存显然进入数据库。我该怎么做呢。

以下是javascript的代码。 (我不知道这是否正确)

$(".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');
    }
})

请帮助我不是很了解代码,因为我只自己定制代码,而且我对这类内容知之甚少。如果我在不勾选复选框的情况下按下借位按钮该怎么办我如何验证这一点,用户在再次按下借用之前会先检查一下。谢谢。

这是我借用的整个代码

<?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') ?>

进行更改并且它现在适用于我想借用它的交易部分的验证部分,但不会保存到数据库但它只提示您已借用 CODE新借用按钮:

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

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>       

2 个答案:

答案 0 :(得分:2)

首先是javascript和jQuery代码。

点击一个bowrrow按钮调用此函数以检查是否选中了复选框。

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

点击按钮调用此功能,如下所示。

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

答案 1 :(得分:1)

正如@ SpYk3HH已经提到过你在这里发布的部分代码不是PHP。但是根据阅读完整的帖子。我猜你在后端(PHP)端收到帖子数据时基本上遇到了一些问题。

实际上,HTML Element Checkbox通常扮演BOOL值(true / false)。因此,如果选中了复选框,您将获得发布数据中的复选框值,否则您将无法收到它。

如何处理这种情况:

嗯,这是与您的代码一起玩的东西,您应该在将数据插入数据库之前创建一个数组:

$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.

此外,您还可以在提交表单之前验证您的复选框:

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