使用Jquery将默认选中的值赋予复选框

时间:2015-10-14 07:22:24

标签: javascript jquery checkbox

我尝试使用jquery将checked属性添加到特定的复选框,但它似乎无法正常工作。

复选框功能显示

function product_category_loop_array ($product_category_array = array (), $parent_id = 0, $margin = -20) {
            if (!empty ($product_category_array[$parent_id])) {
                $margin = $margin + 20;
                foreach ($product_category_array[$parent_id] as $items) {
                    echo "<input style='margin: 0 0 0 ".$margin."px; overflow: hidden;' id='".$items['id']."' class='category_checkbox left' name='product_category[]' type='checkbox' value='".$items['id']."'><label class='category_checkbox_label left'>".$items['name']."</label>
                    <div class='clear'></div>";
                    product_category_loop_array ($product_category_array, $items["id"], $margin);
                }
            }
        }
        function product_category () {
            $db_connect = mysqli_connect (db_host, db_username, db_password, db_name);
            $product_category_query = $db_connect->query ("SELECT id, name, parent_id FROM `product_category` ORDER BY name ASC");
            $product_category_array = array ();
            if (mysqli_num_rows ($product_category_query)) {
                while ($row = mysqli_fetch_array ($product_category_query, MYSQLI_ASSOC)) {
                    $product_category_array[$row['parent_id']][] = $row;
                }
                product_category_loop_array ($product_category_array);
            }
        }

HTML

<div id="tabs-2" class="product_detail hide">
                            <div class="left">
                                <form class="product_detail_form" method="post" action="">
                                    <?php product_category () ?>
                                    <p class="detail_submit"><input name="save_product_category" type="submit" value="Save"></p>
                                </form>
                            </div>
                        </div>

Jquery的

<script>
    $ (document) .ready (function () {
        <?php echo $hide_div ?>;
        var selected_array = <?php echo json_encode ($product_category_selected) ?>;
        $.each (selected_array (key, value) {
            $ ("#"+value).prop("checked", true);
        });
    });
</script>

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

使用

$ ("#"+value).attr("checked", "checked");

答案 1 :(得分:0)

build.gradle