簡體   English   中英

如何確定復選框是否已選中

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

我正在嘗試為借用按鈕創建一個驗證,該借用按鈕在被按下但沒有未被檢查的項目時應該顯示提示,但是正在發生的情況是它不會顯示提示並且也不會保存到數據庫中明顯。 我將如何嘗試做到這一點。

以下是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>       

首先,它是一個javascript和jQuery代碼。

單擊弓箭按鈕以調用此函數,以檢查復選框是否已選中,如下所示。

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();" />

正如@ SpYk3HH已經提到的,您在此處發布的代碼部分不是PHP。 但根據閱讀您的完整文章。 我可以猜測,在后端(PHP)端接收發布數據時,您基本上遇到了一些問題。

實際上, HTML元素復選框通常作為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.

此外,您還可以在提交表單之前驗證您的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