仅用于验证的JavaScript已被检查

时间:2013-03-17 19:43:29

标签: php javascript jquery codeigniter

我有一些使用codeigniter的PHP代码。如何检查标记为检查的字段的验证?我的代码如下。

您能否告诉我如何检查项目是否已被选中并强制进行相应的验证?

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <script type="text/Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>/site.css"></link>
</head>

<body>

    <script type="text/Javascript">

        $(function () {
            $('.checkall').click(function () {
                $(this).parents('fieldset:eq(0)').
                    find(':checkbox').attr('checked', this.checked);
            });
        });

    </script>

    <? $attributes = array('class' => '', 'id' => ''); ?>

    <? echo form_open('ShoppingListController', $attributes); ?>

    <div class="divTable">

        <fieldset>
            <!-- these will be affected by check all -->
            <div class="divRow">Product Name | Quantity | Packages</div>
            <div class="divRow"><input type="checkbox" size="100" class="checkall"> Check all</div>


            <br>

                <?php foreach ($records as $rec) {
                    ?>

                    <div class="divRow"><input type="checkbox">
                            <input size="5" type="hidden" value="<? echo $rec->id; ?>" name="id"></input>
                            <input size="20" type="text" value="<? echo $rec->name; ?>" name="name"></input>
                            <label for="quantity">Value <span class="required">*</span></label>
                            <span class="required"><?php echo form_error('quantity'); ?></span>
                            <input size="5" type="text" value="" name="quantity"></input>

                            <select name="package">
                                <option name="case">Case</option>
                                <option name="box">Box</option>
                                <option name="box">Single Bottle</option>
                            </select>
                    </div>                
                    <br> 
                        <?
                    }
                    ?>


                    </fieldset> 
                    </div>
                    <div><input type="submit" name="submit"/></div>
                    </form>



                    </body>

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

var divRows = $('.divRow');
var count = divRows.length;

divRows.each(function() {
   if ($(this).find('input[type=checkbox]').is(':checked')) {
       count--;
   }
});
if (count != 0) {
   alert(count + ' checkboxes not checked');
}

http://jsfiddle.net/SKgA8/