javascript - 如何判断复选框是否被选中

标签 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');
    }
})

请帮忙,我不太了解代码,因为我只是自己定制代码,而且我对这类东西的了解有限。如果我按下借用按钮而不选中复选框会怎样?我如何验证用户在再次按下借用之前会先检查某些内容。谢谢。

这是我的 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') ?>
<小时/>

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

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

关于javascript - 如何判断复选框是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450529/

相关文章:

javascript - JQuery "greying"在表单中输出文本字段

javascript - Vue + 火力地堡 : Users being created but logged in under other users accounts

javascript - 使用 json_encode 输出换行符或新行?

php - 损坏的嵌套表

jquery - 当侧边栏到达页面底部时停止滚动

jquery - 跨度覆盖 : click will be triggered in IE when it shouldn't

javascript - 如何使用嵌入式 ruby​​ 动态创建 jquery 选择器(在 .js.erb 文件中)?

javascript - AngularJs promise 返回未定义

javascript - 打开一个与父窗口保持相同位置的空白窗口

javascript - 使固定的 div 滚动随页面滚动。 (方形空间)